﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using GDFlacTool.Properties;
using GDFlacTools.Controls.ComboBoxCustom;

namespace GDFlacTool.Forms
{
    public partial class FormConverter : Form
    {
        #region Fields.

        private readonly string _converterPath;
        private readonly Dictionary<string, string> _dspEffects = new Dictionary<string, string>();
        private readonly List<string> _convertFiles;

        private string _mp3Bitrate = string.Empty;
        private int _mp3BitrateValue;
        private string _mp3BitrateDescription;
        private string _mp3Encoding = string.Empty;
        private string _mp3Frequency = string.Empty;
        private string _mp3Channels = string.Empty;
        private string _mp3Copyright = string.Empty;
        private string _mp3Original = string.Empty;
        private string _mp3CrcErrorProtection = string.Empty;
        private string _mp3ForceIsoCompability = string.Empty;

        private string _flacVerify = string.Empty;
        private string _flacCompressionLevel = string.Empty;

        private string _shnEncoderLocation = string.Empty;
        private readonly string _shnSelection = string.Empty;
        private readonly string _shnCliCmd = string.Empty;

        private string _appleLosslessVerify = string.Empty;

        private string _aiffBits = string.Empty;
        private string _aiffChannels = string.Empty;
        private string _aiffFrequency = string.Empty;

        private string _waveBits = string.Empty;
        private string _waveChannels = string.Empty;
        private string _waveFrequency = string.Empty;
        private string _waveCompression = string.Empty;
        
        private string _waveAcm = string.Empty;

        private string _waveMp3 = string.Empty;
        private string _waveMp3Bitrate = string.Empty;
        private string _waveMp3Encoding = string.Empty;
        private string _waveMp3Frequency = string.Empty;
        private string _waveMp3Channels = string.Empty;
        private string _waveMp3Copyright = string.Empty;
        private string _waveMp3Original = string.Empty;
        private string _waveMp3CrcErrorProtection = string.Empty;
        private string _waveMp3ForceIsoCompability = string.Empty;

        private string _oggVorbisBitRate = string.Empty;
        private double _oggVorbisBitRateValue;
        private string _oggVorbisBitRateDescription = string.Empty;
        private string _oggVorbisFrequency = string.Empty;
        private string _oggVorbisChannels = string.Empty;

        private string _neroAacEncoderLocation = string.Empty;
        private string _neroAacBitRate = string.Empty;
        private string _neroAacBitRateValue = string.Empty;
        private string _neroAacProfile = string.Empty;
        private string _neroAacQuality = string.Empty;
        private string _neroAacBitRateDescription = string.Empty;
        private int _neroAacSelectionFirstValue = 0;
        private int _neroAacSelectionSecondValue = 0;
        private int _neroAacSelectionThirdValue = 0;

        #endregion

        #region Constructors.

        public FormConverter(List<string> convertFiles)
        {
            InitializeComponent();
            
            _convertFiles = convertFiles;
            
            string path = Path.Combine(Application.StartupPath, "CoreConverter");
            string shorten = string.Format("encoder{0}Shorten{0}shorten.exe", Path.DirectorySeparatorChar);
            string neroAac = string.Format("encoder{0}m4a Nero (AAC){0}neroAacEnc.exe", Path.DirectorySeparatorChar);
            _converterPath = Path.Combine(path, "CoreConverter.exe");
            _shnEncoderLocation = Path.Combine(path, shorten);
            _neroAacEncoderLocation = Path.Combine(path, neroAac);
            //_startInfo.FileName = _converterPath;
            
            AddCompressionCodecs();

            LoadAiffBitDephtCombo();
            LoadAiffSampleRateCombo();
            LoadAiffChannelsCombo();

            LoadOggVorbisFrequency();
            LoadOggVorbisChannelsCombo();

            LoadWaveBitDephtCombo();
            LoadWaveSampleRateCombo();
            LoadWaveChannelsCombo();

            Width = 490;
            Height = 540;
        }

        #endregion

        #region Form Events.

        private void FormConverter_Load(object sender, EventArgs e)
        {
            lblFileCount.Text = string.Format("Converting {0} Files", _convertFiles.Count);

            cboCompressionCodecs.SelectedIndex = GetIndexForCodec(AudioConverterSettings.Default.ConvertToCodec);

            chkDeleteSourceFiles.Checked = AudioConverterSettings.Default.chkDeleteSourceFiles_Checked;
            rbtSendToRecycleBin.Checked = AudioConverterSettings.Default.rbtSendToRecycleBin_Checked;
            rbtPermanentDelete.Checked = AudioConverterSettings.Default.rbtPermanentDelete_Checked;
            chkNormalizeVolume.Checked = AudioConverterSettings.Default.chkNormalizeVolume_Checked;
            chkPreserveTags.Checked = AudioConverterSettings.Default.chkPreserveTags_Checked;
            txtFolder.Text = AudioConverterSettings.Default.txtFolder_Text;
            rbtOriginalFolder.Checked = AudioConverterSettings.Default.rbtOriginalFolder_Checked;
            rbtFolder.Checked = AudioConverterSettings.Default.rbtFolder_Checked;

            rbtMp3QualityVbr.Checked = AudioConverterSettings.Default.rbtMp3QualityVbr_Checked;
            rbtMp3BitrateAbr.Checked = AudioConverterSettings.Default.rbtMp3BitrateAbr_Checked;
            rbtMp3BitrateCbr.Checked = AudioConverterSettings.Default.rbtMp3BitrateCbr_Checked;
            rbtMp3FreeFormat.Checked = AudioConverterSettings.Default.rbtMp3FreeFormat_Checked;

            cboFlacEncoding.SelectedIndex = AudioConverterSettings.Default.cboEncodingFlac_SelectedIndex;
            chkFlacVerify.Checked = AudioConverterSettings.Default.chkVerifyFlac_Checked;

            chkVerifyAppleLossless.Checked = AudioConverterSettings.Default.chkVerifyAppleLossless_Checked;

            cboAiffBitDepht.SelectedIndex = AudioConverterSettings.Default.cboAiffBitDepht_SelectedIndex;
            cboAiffSampleRate.SelectedIndex = AudioConverterSettings.Default.cboAiffSampleRate_SelectedIndex;
            cboAiffChannels.SelectedIndex = AudioConverterSettings.Default.cboAiffChannels_SelectedIndex;

            _mp3Frequency = AudioConverterSettings.Default.mp3Frequency;
            _mp3Channels = AudioConverterSettings.Default.mp3Channels;
            _mp3Copyright = AudioConverterSettings.Default.mp3Copyright;
            _mp3Original = AudioConverterSettings.Default.mp3Original;
            _mp3CrcErrorProtection = AudioConverterSettings.Default.mp3CrcErrorProtection;
            _mp3ForceIsoCompability = AudioConverterSettings.Default.mp3ForceISOCompability;
            
            chkOggVorbisFinerQuality.Checked = AudioConverterSettings.Default.chkOggVorbisFinerQuality_Checked;
            rbtOggVorbisVbr.Checked = AudioConverterSettings.Default.rbtOggVorbisVbr_Checked;
            rbtOggVorbisAbr.Checked = AudioConverterSettings.Default.rbtOggVorbisAbr_Checked;
            rbtOggVorbisCbr.Checked = AudioConverterSettings.Default.rbtOggVorbisCbr_Checked;
            cboOggVorbisFrequency.SelectedIndex = AudioConverterSettings.Default.cboOggVorbisFrequency_SelectedIndex;
            cboOggVorbisChannels.SelectedIndex = AudioConverterSettings.Default.cboOggVorbisChannels_SelectedIndex;

            rbtWaveCompressed.Checked = AudioConverterSettings.Default.rbtWaveCompressed_Checked;
            rbtWaveUncompressed.Checked = AudioConverterSettings.Default.rbtWaveUncompressed_Checked;
            rbtWave_mp3.Checked = AudioConverterSettings.Default.rbtWave_mp3_Checked;
            cboWaveBitDepht.SelectedIndex = AudioConverterSettings.Default.cboWaveBitDepht_SelectedIndex;
            cboWaveSampleRate.SelectedIndex = AudioConverterSettings.Default.cboWaveSampleRate_SelectedIndex;
            cboWaveChannels.SelectedIndex = AudioConverterSettings.Default.cboWaveChannels_SelectedIndex;

            _waveMp3Bitrate = AudioConverterSettings.Default.waveMp3Bitrate;
            _waveMp3Encoding = AudioConverterSettings.Default.waveMp3Encoding;
            _waveMp3Frequency = AudioConverterSettings.Default.waveMp3Frequency;
            _waveMp3Channels = AudioConverterSettings.Default.waveMp3Channels;
            _waveMp3Copyright = AudioConverterSettings.Default.waveMp3Copyright;
            _waveMp3Original = AudioConverterSettings.Default.waveMp3Original;
            _waveMp3CrcErrorProtection = AudioConverterSettings.Default.waveMp3CrcErrorProtection;
            _waveMp3ForceIsoCompability = AudioConverterSettings.Default.waveMp3ForceISOCompability;

            cboWMCodec.SelectedIndex = AudioConverterSettings.Default.cboWMCodec_SelectedIndex;

            cboTestConversionSpeedLimiter.SelectedIndex = AudioConverterSettings.Default.cboTestConversionSpeedLimiter_SelectedIndex;
            chkTestConversionQualityDecoding.Checked = AudioConverterSettings.Default.chkTestConversionQualityDecoding_Checked;

            rbtNeroAacQualityVbr.Checked = AudioConverterSettings.Default.rbtNeroAacQualityVbr_Checked;
            rbtNeroAacBitrateAbr.Checked = AudioConverterSettings.Default.rbtNeroAacBitrateAbr_Checked;
            rbtNeroAacBitrateCbr.Checked = AudioConverterSettings.Default.rbtNeroAacBitrateCbr_Checked;
            cboNeroAacForcedProfile.SelectedIndex = AudioConverterSettings.Default.cboNeroAacForcedProfile_SelectedIndex;
        }

        private void FormConverter_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer.Enabled = false;

            AudioConverterSettings.Default.chkDeleteSourceFiles_Checked = chkDeleteSourceFiles.Checked;
            AudioConverterSettings.Default.rbtSendToRecycleBin_Checked = rbtSendToRecycleBin.Checked;
            AudioConverterSettings.Default.rbtPermanentDelete_Checked = rbtPermanentDelete.Checked;
            AudioConverterSettings.Default.chkNormalizeVolume_Checked = chkNormalizeVolume.Checked;
            AudioConverterSettings.Default.chkPreserveTags_Checked = chkPreserveTags.Checked;
            AudioConverterSettings.Default.txtFolder_Text = txtFolder.Text;
            AudioConverterSettings.Default.rbtOriginalFolder_Checked = rbtOriginalFolder.Checked;
            AudioConverterSettings.Default.rbtFolder_Checked = rbtFolder.Checked;
            
            AudioConverterSettings.Default.ConvertToCodec = cboCompressionCodecs.Text;
            
            AudioConverterSettings.Default.rbtMp3QualityVbr_Checked = rbtMp3QualityVbr.Checked;
            AudioConverterSettings.Default.rbtMp3BitrateAbr_Checked = rbtMp3BitrateAbr.Checked;
            AudioConverterSettings.Default.rbtMp3BitrateCbr_Checked = rbtMp3BitrateCbr.Checked;
            AudioConverterSettings.Default.rbtMp3FreeFormat_Checked = rbtMp3FreeFormat.Checked;
            

            AudioConverterSettings.Default.cboEncodingFlac_SelectedIndex = cboFlacEncoding.SelectedIndex;
            AudioConverterSettings.Default.chkVerifyFlac_Checked = chkFlacVerify.Checked;

            AudioConverterSettings.Default.chkVerifyAppleLossless_Checked = chkVerifyAppleLossless.Checked;

            AudioConverterSettings.Default.cboAiffBitDepht_SelectedIndex = cboAiffBitDepht.SelectedIndex;
            AudioConverterSettings.Default.cboAiffSampleRate_SelectedIndex = cboAiffSampleRate.SelectedIndex;
            AudioConverterSettings.Default.cboAiffChannels_SelectedIndex = cboAiffChannels.SelectedIndex;

            AudioConverterSettings.Default.rbtOggVorbisVbr_Checked = rbtOggVorbisVbr.Checked;
            AudioConverterSettings.Default.rbtOggVorbisAbr_Checked = rbtOggVorbisAbr.Checked;
            AudioConverterSettings.Default.rbtOggVorbisCbr_Checked = rbtOggVorbisCbr.Checked;
            AudioConverterSettings.Default.cboOggVorbisFrequency_SelectedIndex = cboOggVorbisFrequency.SelectedIndex;
            AudioConverterSettings.Default.cboOggVorbisChannels_SelectedIndex = cboOggVorbisChannels.SelectedIndex;
            AudioConverterSettings.Default.chkOggVorbisFinerQuality_Checked = chkOggVorbisFinerQuality.Checked;

            AudioConverterSettings.Default.rbtWaveCompressed_Checked = rbtWaveCompressed.Checked;
            AudioConverterSettings.Default.rbtWaveUncompressed_Checked = rbtWaveUncompressed.Checked;
            AudioConverterSettings.Default.rbtWave_mp3_Checked = rbtWave_mp3.Checked;
            AudioConverterSettings.Default.cboWaveBitDepht_SelectedIndex = cboWaveBitDepht.SelectedIndex;
            AudioConverterSettings.Default.cboWaveSampleRate_SelectedIndex = cboWaveSampleRate.SelectedIndex;
            AudioConverterSettings.Default.cboWaveChannels_SelectedIndex = cboWaveChannels.SelectedIndex;

            AudioConverterSettings.Default.mp3Frequency = _mp3Frequency;
            AudioConverterSettings.Default.mp3Channels = _mp3Channels;
            AudioConverterSettings.Default.mp3Copyright = _mp3Copyright;
            AudioConverterSettings.Default.mp3Original = _mp3Original;
            AudioConverterSettings.Default.mp3CrcErrorProtection = _mp3CrcErrorProtection;
            AudioConverterSettings.Default.mp3ForceISOCompability = _mp3ForceIsoCompability;

            AudioConverterSettings.Default.waveMp3Bitrate = _waveMp3Bitrate;
            AudioConverterSettings.Default.waveMp3Encoding = _waveMp3Encoding;
            AudioConverterSettings.Default.waveMp3Frequency = _waveMp3Frequency;
            AudioConverterSettings.Default.waveMp3Channels = _waveMp3Channels;
            AudioConverterSettings.Default.waveMp3Copyright = _waveMp3Copyright;
            AudioConverterSettings.Default.waveMp3Original = _waveMp3Original;
            AudioConverterSettings.Default.waveMp3CrcErrorProtection = _waveMp3CrcErrorProtection;
            AudioConverterSettings.Default.waveMp3ForceISOCompability = _waveMp3ForceIsoCompability;

            AudioConverterSettings.Default.cboWMCodec_SelectedIndex = cboWMCodec.SelectedIndex;

            AudioConverterSettings.Default.cboTestConversionSpeedLimiter_SelectedIndex = cboTestConversionSpeedLimiter.SelectedIndex;
            AudioConverterSettings.Default.chkTestConversionQualityDecoding_Checked = chkTestConversionQualityDecoding.Checked;

            AudioConverterSettings.Default.rbtNeroAacQualityVbr_Checked = rbtNeroAacQualityVbr.Checked;
            AudioConverterSettings.Default.rbtNeroAacBitrateAbr_Checked = rbtNeroAacBitrateAbr.Checked;
            AudioConverterSettings.Default.rbtNeroAacBitrateCbr_Checked = rbtNeroAacBitrateCbr.Checked;
            AudioConverterSettings.Default.cboNeroAacForcedProfile_SelectedIndex = cboNeroAacForcedProfile.SelectedIndex;

            AudioConverterSettings.Default.Save();
        }

        #endregion

        #region Button Events.

        private void btnCancel_Click(object sender, EventArgs e)
        {
            timer.Enabled = false;
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            if (btnConvert.Text == @"Convert")
            {
                timer.Enabled = false;

                DisableAllControls((ControlCollection) this.Controls);

                ConvertAudioFiles();
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            folderBrowser.Description = @"Select Where To Save Converted Files";

            if (Directory.Exists(txtFolder.Text))
            {
                folderBrowser.SelectedPath = txtFolder.Text;
            }
            else
            {
                folderBrowser.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
            }

            DialogResult result = folderBrowser.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                txtFolder.Text = folderBrowser.SelectedPath;
            }
        }

        #endregion

        #region CheckBox Events.

        private void chkDeleteSourceFiles_CheckedChanged(object sender, EventArgs e)
        {
            const string key = "Delete Source File";

            if (chkDeleteSourceFiles.Checked)
            {
                panDeleteSourceFiles.Enabled = true;
                _dspEffects.Remove(key);
                
                if (rbtSendToRecycleBin.Checked)
                {
                    _dspEffects.Add(key, "\"Delete Source File=-recycle\"");
                }
                else
                {
                    _dspEffects.Add(key, "\"Delete Source File=\"");
                }
            }
            else
            {
                panDeleteSourceFiles.Enabled = false;

                if (_dspEffects.ContainsKey(key))
                {
                    _dspEffects.Remove(key);
                }
            }
        }

        private void chkNormalizeVolume_CheckedChanged(object sender, EventArgs e)
        {
            const string key = "Volume Normalize";

            if (chkNormalizeVolume.Checked)
            {
                _dspEffects.Remove(key);
                _dspEffects.Add(key, "Volume Normalize= -mode={qt}peak{qt} -maxamp={qt}8{qt} -desiredb={qt}0{qt} -adapt_wnd={qt}6000{qt} -fixed={qt}0{qt}");
            }
            else
            {
                if (_dspEffects.ContainsKey(key))
                {
                    _dspEffects.Remove(key);
                }
            }
        }

        #endregion

        #region Other Events.

        private void timer_Tick(object sender, EventArgs e)
        {
            string settings = string.Empty;
            switch (cboCompressionCodecs.Text)
            {
                case "Aiff":
                    settings = GetAiffConversionSettings();
                    break;
                case "Apple Lossless":
                    settings = GetAppleLosslessConversionSettings();
                    break;
                case "FLAC":
                    settings = GetFlacConversionSetting();
                    break;
                case "mp3 (Lame)":
                    settings = GetMp3ConversionSetting();
                    break;
                case "Shorten":
                    settings = GetShortenConversionSettings();
                    break;
                case "ogg vorbis":
                    settings = GetOggVorbisConversionSettings();
                    break;
                case "wave":
                    settings = GetWaveConversionSettings();
                    break;
                case "Windows Media Audio 10":
                    settings = GetWindowsMediaAudioConversionSettings();
                    break;
                case "Test Conversion":
                    settings = GetTestConversionSettings();
                    break;
                case "m4a Nero (AAC)":
                    settings = GetNeroAacConversionSettings();
                    break;
            }

            txtEncodingParameters.Text = string.Format("{0}", settings);
        }

        private void cboCompressionCodecs_SelectedIndexChanged(object sender, EventArgs e)
        {
            string codec = cboCompressionCodecs.Text;

            if (codec == "Test Conversion")
            {
                rbtOriginalFolder.Enabled = false;
                rbtFolder.Enabled = false;
                txtFolder.Enabled = false;
                btnBrowse.Enabled = false;
            }
            else
            {
                if (AudioConverterSettings.Default.rbtFolder_Checked)
                {
                    rbtFolder.Enabled = true;
                    txtFolder.Enabled = true;
                    btnBrowse.Enabled = true;
                }
                else
                {
                    rbtOriginalFolder.Enabled = true;
                }
            }

            SelectPanel(codec);
        }

        private void rbtFolder_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtFolder.Checked)
            {
                txtFolder.Enabled = true;
                btnBrowse.Enabled = true;
            }

            AudioConverterSettings.Default.rbtFolder_Checked = rbtFolder.Checked;
        }

        private void rbtOriginalFolder_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtOriginalFolder.Checked)
            {
                txtFolder.Enabled = false;
                btnBrowse.Enabled = false;
            }

            AudioConverterSettings.Default.rbtOriginalFolder_Checked = rbtOriginalFolder.Checked;
        }

        #endregion

        #region Convert Audio File.

        private void ConvertAudioFiles()
        {
            var files = new List<string>(_convertFiles);
            string toPath;
            string setting = string.Empty;
            string extension = string.Empty;
            string codec = string.Empty;
            var processes = new Process[files.Count];
            int counter = 0;

            if (rbtFolder.Checked && Directory.Exists(txtFolder.Text))
            {
                toPath = txtFolder.Text;
            }
            else
            {
                toPath = Path.GetDirectoryName(files[0]);
            }

            switch (cboCompressionCodecs.Text)
            {
                case "mp3 (Lame)":
                    setting = GetMp3ConversionSetting();
                    extension = "mp3";
                    codec = "mp3 (Lame)";
                    break;
                case "FLAC":
                    setting = GetFlacConversionSetting();
                    extension = "flac";
                    codec = "FLAC";
                    break;
                case "Shorten":
                    setting = GetShortenConversionSettings();
                    extension = "shn";
                    codec = "Shorten";
                    break;
                case "Apple Lossless":
                    setting = GetAppleLosslessConversionSettings();
                    extension = "m4a";
                    codec = "Apple Lossless";
                    break;
                case "Aiff":
                    setting = GetAiffConversionSettings();
                    extension = "aif";
                    codec = "Aiff";
                    break;
                case "ogg vorbis":
                    setting = GetOggVorbisConversionSettings();
                    extension = "ogg";
                    codec = "ogg vorbis";
                    break;
                case "wave":
                    setting = GetWaveConversionSettings();
                    extension = "wav";
                    codec = "wave";
                    break;
                case "Windows Media Audio 10":
                    setting = GetWindowsMediaAudioConversionSettings();
                    extension = "wma";
                    codec = "Windows Media Audio 10";
                    break;
                case "Test Conversion":
                    setting = GetTestConversionSettings();
                    extension = "tcv";
                    codec = "Test Conversion";
                    break;
                case "m4a Nero (AAC)":
                    setting = GetNeroAacConversionSettings();
                    extension = "m4a";
                    codec = "m4a Nero (AAC)";
                    break;
            }

            foreach (var file in files)
            {
                string fromFile = file;
                string filename = string.Format("{0}.{1}", Path.GetFileNameWithoutExtension(fromFile), extension);
                
                if (toPath != null)
                {
                    string toFile = Path.Combine(toPath, filename);
                    string ifile = string.Format("-infile=\"{0}\"", fromFile);
                    string ofile = string.Format("-outfile=\"{0}\"", toFile);
                    string dspef = AddDspEffects();
                    string cnvto = string.Format("-convert_to=\"{0}\"", codec);
                    string clist = string.Format("{0}", setting);

                    if (!chkPreserveTags.Checked)
                    {
                        cnvto = string.Format("{0} -noidtag", cnvto);
                    }

                    string args = string.Format(" {0} {1} {2} {3} {4}", ifile, ofile, dspef, cnvto, clist);

                    var proc = new Process();

                    //proc.StartInfo.UseShellExecute = false;

                    proc.StartInfo.FileName = _converterPath;

                    //proc.StartInfo.CreateNoWindow = true;

                    proc.StartInfo.Arguments = args;

                    processes[counter] = proc;

                    counter++;
                }
            }

            var cts = new CancellationTokenSource();
            var po = new ParallelOptions();

            po.CancellationToken = cts.Token;
            po.MaxDegreeOfParallelism = GetNumberOfCores();

            Task.Factory.StartNew(() =>
            {
                //if (_cancellation)
                //cts.Cancel();
            });

            try
            {
                    Parallel.ForEach(processes, po, proc =>
                    {
                        proc.Start();
                        proc.WaitForExit();
                        po.CancellationToken.ThrowIfCancellationRequested();
                    });
            }
            catch (Exception e)
            {
                string msg = string.Format("There was a problem converting the selected audio files\n\n{0}", e.Message);

                MessageBox.Show(this, msg, @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                btnCancel.Enabled = true;
            }

            //btnConvert.Enabled = true;
            
            //btnConvert.Text = @"Finished";

            //btnConvert.DialogResult = DialogResult.OK;
            DialogResult = DialogResult.OK;

            this.Close();
        }

        private int GetNumberOfCores()
        {
            int coreCount = new ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get().Cast<ManagementBaseObject>().Sum(item => int.Parse(item["NumberOfLogicalProcessors"].ToString()));

            return coreCount;
        }

        #endregion

        #region Audio Convert Settings.

        private string AddDspEffects()
        {
            int counter = 1;
            string dspSetting = string.Empty;

            if (_dspEffects.Count == 0)
            {
                return string.Empty;
            }

            foreach (var row in _dspEffects)
            {
                string dsp = string.Format(" -dspeffect{0}=", counter);
                string dspEffect = string.Format("{0}{1}", dsp, row.Value);

                dspSetting += dspEffect;

                counter++;
            }

            return dspSetting;
        }

        private string GetMp3ConversionSetting()
        {
            var sb = new StringBuilder();
            var settings = new []{_mp3Bitrate, _mp3Encoding, _mp3Frequency, _mp3Channels, _mp3Copyright, _mp3Original, _mp3CrcErrorProtection, _mp3ForceIsoCompability};

            for (int i = 0; i < settings.Length; i++)
            {
                if (!string.IsNullOrEmpty(settings[i]))
                {
                    if (i == 0)
                        sb.Append(settings[0]);
                    else
                        sb.AppendFormat(" {0}", settings[i]);
                }   
            }

            string setting = sb.ToString();

            setting = setting.Replace("  ", " ");
            setting = setting.Trim();

            return setting;
        }

        private string GetFlacConversionSetting()
        {
            var sb = new StringBuilder();
            var settings = new[] { _flacCompressionLevel, _flacVerify };

            for (int i = 0; i < settings.Length; i++)
            {
                if (!string.IsNullOrEmpty(settings[i]))
                {
                    if (i == 0)
                        sb.Append(settings[0]);
                    else
                        sb.AppendFormat(" {0}", settings[i]);
                }
            }

            string setting = sb.ToString();

            setting = setting.Replace("  ", " ");
            setting = setting.Trim();

            return setting;
        }

        private string GetShortenConversionSettings()
        {
            string cliEncoder;
            string cliCmd;
            const string cmd = "{qt}[infile]{qt} {qt}[outfile]{qt}";
            string selection;

            if (!string.IsNullOrEmpty(_shnEncoderLocation) && File.Exists(_shnEncoderLocation))
            {
                cliEncoder = string.Format(" -cli_encoder=\"{0}\"", _shnEncoderLocation);
            }
            else
            {
                cliEncoder = string.Format(" -cli_encoder=\"{0}\"", string.Empty);
            }
                
            if (!string.IsNullOrEmpty(_shnCliCmd))
            {
                cliCmd = string.Format(" -{0}", _shnCliCmd);
            }
            else
            {
                cliCmd = string.Format(" -cli_cmd=\" {0}\"", cmd);
            }

            if (!string.IsNullOrEmpty(_shnSelection))
            {
                selection = string.Format(" -{0}", _shnSelection);
            }
            else
            {
                selection = " -selection=\"\"";
            }

            return string.Format("{0}{1}{2}", cliEncoder, cliCmd, selection);
        }

        private string GetAppleLosslessConversionSettings()
        {
            return _appleLosslessVerify;
        }

        private string GetAiffConversionSettings()
        {
            var sb = new StringBuilder();

            sb.AppendFormat(" {0} {1} {2}", _aiffBits, _aiffFrequency, _aiffChannels);

            string setting = sb.ToString();

            setting = setting.Replace("  ", " ");
            setting = setting.TrimEnd();

            return setting;
        }

        private string GetOggVorbisConversionSettings()
        {
            var sb = new StringBuilder();

            sb.AppendFormat("{0} {1} {2}", _oggVorbisBitRate, _oggVorbisFrequency, _oggVorbisChannels);

            string setting = sb.ToString();

            setting = setting.Replace("  ", " ");
            setting = setting.Trim();

            return setting;
        }

        private string GetWaveConversionSettings()
        {
            var sb = new StringBuilder();

            if (rbtWaveUncompressed.Checked)
            {
                sb.AppendFormat("{0} {1} {2} {3}", _waveCompression, _waveBits, _waveFrequency, _waveChannels);
            }

            if (rbtWaveCompressed.Checked)
            {
                sb.AppendFormat("{0} {1}", _waveCompression, _waveAcm);
            }

            if (rbtWave_mp3.Checked)
            {
                _waveMp3 = string.Format("-wavemp3=\"{0} {1} {2} {3} {4} {5} {6} {7}", _waveMp3Bitrate, _waveMp3Encoding,
                                         _waveMp3Frequency, _waveMp3Channels, _waveMp3Copyright, _waveMp3Original,
                                         _waveMp3CrcErrorProtection, _waveMp3ForceIsoCompability);

                _waveMp3 = _waveMp3.TrimEnd();

                if (_waveMp3 != "-wavemp3=\"")
                {
                    _waveMp3 = _waveMp3.Replace("  ", " ");
                    _waveMp3 = _waveMp3.Trim();

                    if (!_waveMp3.EndsWith("\""))
                    {
                        _waveMp3 = string.Format("{0}\"", _waveMp3);
                    }

                    sb.AppendFormat("{0} {1}", _waveCompression, _waveMp3);
                }
                else
                {
                    sb.AppendFormat("{0}", _waveCompression);
                }
            }

            string setting = sb.ToString();

            setting = setting.Replace("  ", " ");
            setting = setting.Trim();

            return setting;
        }

        private string GetWindowsMediaAudioConversionSettings()
        {
            string setting = string.Empty;

            switch (cboWMCodec.Text)
            {
                case "Windows Media Audio Voice 9":
                    setting = GetWindowsMediaAudioVoice9ConversionSettings();
                    break;
                case "Windows Media Audio 9.2 Lossless":
                    setting = GetWindowsMediaAudio92LosslessConversionSettings();
                    break;
                case "Windows Media Audio 9.2":
                    setting = GetWindowsMediaAudio92ConversionSettings();
                    break;
                case "Windows Media Audio 10 Professional":
                    setting = GetWindowsMediaAudio10ProfessionalConversionSettings();
                    break;
            }

            return setting;
        }

        private string GetWindowsMediaAudioVoice9ConversionSettings()
        {
            var sb = new StringBuilder();

            string codec = cboWMCodec.Text;
            string bitrate = cboWMBitrate.Text;
            string frequency = cboWMFrequency.Text;
            string channels = cboWMChannels.Text;

            sb.AppendFormat("-codec=\"{0}\"", codec);

            sb.AppendFormat(" -settings=\"{0}, {1}, {2}\"", bitrate, frequency, channels);

            sb.AppendFormat(" -cbr");

            string setting = sb.ToString();

            setting = setting.Replace("  ", " ");
            setting = setting.Trim();

            return setting;
        }

        private string GetWindowsMediaAudio92LosslessConversionSettings()
        {
            var sb = new StringBuilder();

            string codec = cboWMCodec.Text;
            string bitrate = cboWMBitrate.Text;
            string frequency = cboWMFrequency.Text;
            string channels = cboWMChannels.Text;

            sb.AppendFormat("-codec=\"{0}\"", codec);

            sb.AppendFormat(" -settings=\"{0}, {1}, {2}\"", bitrate, frequency, channels);

            sb.AppendFormat(" -vbr");

            if (chkWMVerify.Checked)
            {
                sb.AppendFormat(" -verify");
            }

            string setting = sb.ToString();

            setting = setting.Replace("  ", " ");
            setting = setting.Trim();

            return setting;
        }

        private string GetWindowsMediaAudio92ConversionSettings()
        {
            var sb = new StringBuilder();

            string codec = cboWMCodec.Text;
            string bitrate = cboWMBitrate.Text;
            string frequency = cboWMFrequency.Text;
            string channels = cboWMChannels.Text;

            sb.AppendFormat("-codec=\"{0}\"", codec);

            if (rbtWMBitrateCbr.Checked)
            {
                sb.AppendFormat(" -settings=\" {0}, {1}, {2}\"", bitrate, frequency, channels);
            }
            else
            {
                sb.AppendFormat(" -settings=\"{0}, {1}, {2}\"", bitrate, frequency, channels);
            }

            if (rbtWMQualityVbr.Checked)
            {
                sb.AppendFormat(" -vbr");
            }
            else
            {
                sb.AppendFormat(" -cbr");
            }

            if (chkWM2PassEncoding.Checked)
            {
                sb.AppendFormat(" -2pass");
            }

            string setting = sb.ToString();

            setting = setting.Replace("  ", " ");
            setting = setting.Trim();

            return setting;
        }

        private string GetWindowsMediaAudio10ProfessionalConversionSettings()
        {
            var sb = new StringBuilder();

            string codec = cboWMCodec.Text;
            string bitrate = cboWMBitrate.Text;
            string frequency = cboWMFrequency.Text;
            string channels = cboWMChannels.Text;

            sb.AppendFormat("-codec=\"{0}\"", codec);

            sb.AppendFormat(" -settings=\"{0}, {1}, {2}\"", bitrate, frequency, channels);

            if (rbtWMQualityVbr.Checked)
            {
                sb.AppendFormat(" -vbr");
            }
            else
            {
                sb.AppendFormat(" -cbr");
            }

            if (chkWM2PassEncoding.Checked)
            {
                sb.AppendFormat(" -2pass");
            }

            string setting = sb.ToString();

            setting = setting.Replace("  ", " ");
            setting = setting.Trim();

            return setting;
        }

        private string GetTestConversionSettings()
        {
            var sb = new StringBuilder();

            if (chkTestConversionQualityDecoding.Checked)
            {
                sb.AppendFormat("-speed=\"{0}\" -hi_quality", cboTestConversionSpeedLimiter.SelectedIndex);
            }
            else
            {
                sb.AppendFormat("-speed=\"{0}\"", cboTestConversionSpeedLimiter.SelectedIndex);
            }

            string setting = sb.ToString();

            setting = setting.Replace("  ", " ");
            setting = setting.Trim();

            return setting;
        }

        private string GetNeroAacConversionSettings()
        {
            var sb = new StringBuilder();

            string cliEncoder;
            string cliCommand;
            string selection;
            const string infile = "{qt}[outfile]{qt}";

            if (!string.IsNullOrEmpty(_shnEncoderLocation) && File.Exists(_shnEncoderLocation))
            {
                cliEncoder = string.Format(" -cli_encoder=\"{0}\"", _neroAacEncoderLocation);
            }
            else
            {
                cliEncoder = string.Format(" -cli_encoder=\"{0}\"", string.Empty);
            }

            cliCommand = string.Format("-cli_cmd=\"-{0} {1} {2} -ignorelength -if - -of {3}\"", _neroAacQuality, _neroAacBitRateValue, _neroAacProfile, infile);

            selection = string.Format("-selection=\"{0},{1},{2}\"", _neroAacSelectionFirstValue, _neroAacSelectionSecondValue, _neroAacSelectionThirdValue);

            sb.AppendFormat("{0} {1} {2}", cliEncoder, cliCommand, selection);

            string setting = sb.ToString();

            setting = setting.Replace("  ", " ");
            setting = setting.TrimEnd();

            return setting;
        }

        #endregion

        #region Add Compression Codecs.

        private void AddCompressionCodecs()
        {
            cboCompressionCodecs.Add(imlCodecs.Images["Aiff"], "Aiff");
            cboCompressionCodecs.Add(imlCodecs.Images["Apple Lossless"], "Apple Lossless");
            cboCompressionCodecs.Add(imlCodecs.Images["FLAC"], "FLAC");
            cboCompressionCodecs.Add(imlCodecs.Images["mp3 (Lame)"], "mp3 (Lame)");
            cboCompressionCodecs.Add(imlCodecs.Images["Shorten"], "Shorten");
            cboCompressionCodecs.Add(imlCodecs.Images["ogg vorbis"], "ogg vorbis");
            cboCompressionCodecs.Add(imlCodecs.Images["wave"], "wave");
            cboCompressionCodecs.Add(imlCodecs.Images["Windows Media Audio 10"], "Windows Media Audio 10");
            cboCompressionCodecs.Add(imlCodecs.Images["Test Conversion"], "Test Conversion");
            cboCompressionCodecs.Add(imlCodecs.Images["m4a Nero (AAC)"], "m4a Nero (AAC)");
        }

        private int GetIndexForCodec(string codec)
        {
            int index = cboCompressionCodecs.Items.IndexOf(codec);

            if (index < 0)
            {
                index = 0;
            }

            return index;
        }

        #endregion

        #region Move Panels.

        private void SelectPanel(string selectedCodec)
        {
            var hidePoint = new Point(700, 60);
            var showPoint = new Point(25, 100);

            foreach (Control ctrl in Controls)
            {
                if (ctrl.GetType() == typeof (Panel))
                {
                    if (ctrl.Name != "panDeleteSourceFiles")
                        ctrl.Location = hidePoint;
                }
            }

            switch (selectedCodec)
            {
                case "FLAC":
                    panFlac.Location = showPoint;
                    break;
                case "mp3 (Lame)":
                    panMp3.Location = showPoint;
                    break;
                case "Shorten":
                    panShn.Location = showPoint;
                    break;
                case "Apple Lossless":
                    panAppleLossless.Location = showPoint;
                    break;
                case "Aiff":
                    panAiff.Location = showPoint;
                    break;
                case "ogg vorbis":
                    panOggVorbis.Location = showPoint;
                    break;
                case "wave":
                    panWave.Location = showPoint;
                    break;
                case "Windows Media Audio 10":
                    panWindowsMedia.Location = showPoint;
                    break;
                case "Test Conversion":
                    panTestConversion.Location = showPoint;
                    break;
                case "m4a Nero (AAC)":
                    panNeroAac.Location = showPoint;
                    break;
            }
        }

        #endregion

        #region Panel MP3 Events.

        private void cboMp3Encoding_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboMp3Encoding.SelectedIndex)
            {
                case 0:
                    _mp3Encoding = string.Format("-encoding=\"SLOW\"");
                    break;
                case 1:
                    _mp3Encoding = string.Empty;
                    break;
                case 2:
                    _mp3Encoding = string.Format("-encoding=\"FAST\"");
                    break;
                default:
                    _mp3Encoding = string.Empty;
                    break;
            }

            if (rbtMp3BitrateCbr.Checked)
            {
                AudioConverterSettings.Default.cboMp3EncodingCBR_SelectedIndex = cboMp3Encoding.SelectedIndex;
            }

            if (rbtMp3BitrateAbr.Checked)
            {
                AudioConverterSettings.Default.cboMp3EncodingABR_SelectedIndex = cboMp3Encoding.SelectedIndex;
            }

            if (rbtMp3QualityVbr.Checked)
            {
                AudioConverterSettings.Default.cboMp3EncodingVBR_SelectedIndex = cboMp3Encoding.SelectedIndex;
            }

            if (rbtMp3FreeFormat.Checked)
            {
                AudioConverterSettings.Default.cboMp3EncodingFreeFormat_SelectedIndex = cboMp3Encoding.SelectedIndex;
            }
        }

        private void rbtMp3BitrateCbr_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtMp3BitrateCbr.Checked)
            {
                trbMp3Bitrate.Minimum = 1;
                trbMp3Bitrate.Maximum = 18;
                trbMp3Bitrate.TickFrequency = 1;
                trbMp3Bitrate.Value = AudioConverterSettings.Default.trbMp3BitrateCBR_Value;
                ConvertMp3AbrCbrValues();
                cboMp3Encoding.SelectedIndex = AudioConverterSettings.Default.cboMp3EncodingCBR_SelectedIndex;
            }
        }

        private void rbtMp3BitrateAbr_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtMp3BitrateAbr.Checked)
            {
                trbMp3Bitrate.Minimum = 1;
                trbMp3Bitrate.Maximum = 18;
                trbMp3Bitrate.TickFrequency = 1;
                trbMp3Bitrate.Value = AudioConverterSettings.Default.trbMp3BitrateABR_Value;
                ConvertMp3AbrCbrValues();
                cboMp3Encoding.SelectedIndex = AudioConverterSettings.Default.cboMp3EncodingABR_SelectedIndex;
            }
        }

        private void rbtMp3QualityVbr_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtMp3QualityVbr.Checked)
            {
                trbMp3Bitrate.Minimum = 1;
                trbMp3Bitrate.Maximum = 19;
                trbMp3Bitrate.TickFrequency = 1;
                trbMp3Bitrate.Value = AudioConverterSettings.Default.trbMp3BitrateVBR_Value;
                ConvertMp3VbrValues();
                cboMp3Encoding.SelectedIndex = AudioConverterSettings.Default.cboMp3EncodingVBR_SelectedIndex;
            }
        }

        private void rbtMp3FreeFormat_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtMp3FreeFormat.Checked)
            {
                trbMp3Bitrate.Minimum = 320;
                trbMp3Bitrate.Maximum = 640;
                trbMp3Bitrate.TickFrequency = 10;
                trbMp3Bitrate.Value = AudioConverterSettings.Default.trbMp3BitrateFreeFormat_Value;
                ConvertMp3FreeFormatValues();
                cboMp3Encoding.SelectedIndex = AudioConverterSettings.Default.cboMp3EncodingFreeFormat_SelectedIndex;
            }
        }

        private void trbMp3Bitrate_ValueChanged(object sender, EventArgs e)
        {
            if (rbtMp3BitrateCbr.Checked)
            {
                ConvertMp3AbrCbrValues();
                AudioConverterSettings.Default.trbMp3BitrateCBR_Value = trbMp3Bitrate.Value;
            }

            if (rbtMp3BitrateAbr.Checked)
            {
                ConvertMp3AbrCbrValues();
                AudioConverterSettings.Default.trbMp3BitrateABR_Value = trbMp3Bitrate.Value;
            }

            if (rbtMp3QualityVbr.Checked)
            {
                ConvertMp3VbrValues();
                AudioConverterSettings.Default.trbMp3BitrateVBR_Value = trbMp3Bitrate.Value;
            }

            if (rbtMp3FreeFormat.Checked)
            {
                ConvertMp3FreeFormatValues();
                AudioConverterSettings.Default.trbMp3BitrateFreeFormat_Value = trbMp3Bitrate.Value;
            }
        }

        private void btnMp3Advanced_Click(object sender, EventArgs e)
        {
            var frm = new FormConverterMp3(false);

            DialogResult result = frm.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                _mp3Frequency = frm.Frequency;
                _mp3Channels = frm.Channels;
                _mp3Copyright = frm.Copyright;
                _mp3Original = frm.Original;
                _mp3CrcErrorProtection = frm.CrcErrorProtection;
                _mp3ForceIsoCompability = frm.ForceIsoCompability;
            }
        }

        #endregion
        
        #region Panel FLAC Events.

        private void chkFlacVerify_CheckedChanged(object sender, EventArgs e)
        {
            if (chkFlacVerify.Checked)
            {
                _flacVerify = string.Format("-verify");
            }
            else
            {
                _flacVerify = string.Empty;
            }
        }

        private void cboFlacEncoding_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboFlacEncoding.SelectedIndex == 9)
            {
                _flacCompressionLevel = string.Format("-uncompressed");
            }
            else
            {
                _flacCompressionLevel = string.Format("-compression-level-{0}", cboFlacEncoding.SelectedIndex);
            }
        }

        #endregion

        #region Panel SHN Events.

        private void btnShnEncoder_MouseClick(object sender, MouseEventArgs e)
        {
            if (!string.IsNullOrEmpty(_shnEncoderLocation))
            {
                mnuShnCurrentEncoder.Text = string.Format("Current Encoder {0}", _shnEncoderLocation);
            }
            else
            {
                mnuShnCurrentEncoder.Text = string.Format("Current Encoder");
            }

            ctmShorten.Show(btnShnEncoder, e.X, e.Y);
        }

        private void mnuLocateEncoder_Click(object sender, EventArgs e)
        {
            openFileDialog.Title = @"Choose Shorten Encoder";
            openFileDialog.Filter = @"CLI Encoder|shorten.exe";
            openFileDialog.FilterIndex = 1;
            string path = Path.GetDirectoryName(_shnEncoderLocation);

            if (!string.IsNullOrEmpty(path) && Directory.Exists(path))
            {
                openFileDialog.InitialDirectory = Path.GetDirectoryName(_shnEncoderLocation);
            }
            else
            {
                openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
            }

            DialogResult result = openFileDialog.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                _shnEncoderLocation = openFileDialog.FileName;

                //if (SetInstalledShortenDecoder())
                //{
                mnuShnCurrentEncoder.Text = string.Format("Current Encoder {0}", _shnEncoderLocation);
                //}
            }
        }

        #endregion

        #region Panel Apple Lossless Events.

        private void chkVerifyAppleLossless_CheckedChanged(object sender, EventArgs e)
        {
            if (chkVerifyAppleLossless.Checked)
            {
                _appleLosslessVerify = string.Format("-v");
            }
            else
            {
                _appleLosslessVerify = string.Empty;
            }
        }

        #endregion

        #region Panel Aiff Events.

        private void cboAiffBitDepht_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboAiffBitDepht.Text)
            {
                case "[as source]":
                    _aiffBits = string.Empty;
                    break;
                case "8 bit":
                    _aiffBits = string.Format("-bits=\"8\"");
                    break;
                case "16 bit (CD)":
                    _aiffBits = string.Format("-bits=\"16\"");
                    break;
                case "24 bit (DVD)":
                    _aiffBits = string.Format("-bits=\"24\"");
                    break;
                case "32 bit":
                    _aiffBits = string.Format("-bits=\"32\"");
                    break;
                default:
                    _aiffBits = string.Empty;
                    break;
            }
        }

        private void cboAiffSampleRate_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboAiffSampleRate.Text)
            {
                case "[as source]":
                    _aiffFrequency = string.Empty;
                    break;
                case "8 KHz":
                    _aiffFrequency = string.Format("-freq=\"8000\"");
                    break;
                case "11 KHz":
                    _aiffFrequency = string.Format("-freq=\"11000\"");
                    break;
                case "12 KHz":
                    _aiffFrequency = string.Format("-freq=\"12000\"");
                    break;
                case "16 KHz":
                    _aiffFrequency = string.Format("-freq=\"16000\"");
                    break;
                case "22 KHz":
                    _aiffFrequency = string.Format("-freq=\"22000\"");
                    break;
                case "24 KHz":
                    _aiffFrequency = string.Format("-freq=\"24000\"");
                    break;
                case "32 KHz":
                    _aiffFrequency = string.Format("-freq=\"32000\"");
                    break;
                case "44,1 KHz (CD)":
                    _aiffFrequency = string.Format("-freq=\"44100\"");
                    break;
                case "48 KHz (DAT)":
                    _aiffFrequency = string.Format("-freq=\"48000\"");
                    break;
                case "96 KHz (DVD)":
                    _aiffFrequency = string.Format("-freq=\"96000\"");
                    break;
                case "192 KHz":
                    _aiffFrequency = string.Format("-freq=\"192000\"");
                    break;
                default:
                    _aiffFrequency = string.Empty;
                    break;
            }
        }

        private void cboAiffChannels_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboAiffChannels.Text)
            {
                case "[as source]":
                    _aiffChannels = string.Empty;
                    break;
                case "1 'mono'":
                    _aiffChannels = string.Format("-channels=\"1\"");
                    break;
                case "2 'stereo'":
                    _aiffChannels = string.Format("-channels=\"2\"");
                    break;
                case "3":
                    _aiffChannels = string.Format("-channels=\"3\"");
                    break;
                case "4 'quadraphonic'":
                    _aiffChannels = string.Format("-channels=\"4\"");
                    break;
                case "5 'surround'":
                    _aiffChannels = string.Format("-channels=\"5\"");
                    break;
                case "6 '5.1 surround' (DVD)":
                    _aiffChannels = string.Format("-channels=\"6\"");
                    break;
                case "7":
                    _aiffChannels = string.Format("-channels=\"7\"");
                    break;
                case "8 '7.1 theater'":
                    _aiffChannels = string.Format("-channels=\"8\"");
                    break;
            }
        }

        #endregion

        #region Panel Ogg Vorbis Events.

        private void rbtOggVorbisVbr_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtOggVorbisVbr.Checked)
            {
                if (chkOggVorbisFinerQuality.Checked)
                {
                    trbOggVorbisBitRate.Minimum = 0;
                    trbOggVorbisBitRate.Maximum = 44;
                    trbOggVorbisBitRate.TickFrequency = 1;
                    trbOggVorbisBitRate.Value = AudioConverterSettings.Default.trbOggVorbisBitRateVbrFiner_Value;
                    ConvertOggVorbisVbrFinerValues();
                }
                else
                {
                    trbOggVorbisBitRate.Minimum = 0;
                    trbOggVorbisBitRate.Maximum = 11;
                    trbOggVorbisBitRate.TickFrequency = 1;
                    trbOggVorbisBitRate.Value = AudioConverterSettings.Default.trbOggVorbisBitRateVbr_Value;
                    ConvertOggVorbisVbrValues();
                }
                
                chkOggVorbisFinerQuality.Enabled = true;
                lblOggVorbisInfo.Visible = true;
            }
        }

        private void rbtOggVorbisAbr_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtOggVorbisAbr.Checked)
            {
                trbOggVorbisBitRate.Minimum = 0;
                trbOggVorbisBitRate.Maximum = 11;
                trbOggVorbisBitRate.TickFrequency = 1;

                trbOggVorbisBitRate.Value = AudioConverterSettings.Default.trbOggVorbisBitRateAbr_Value;
                ConvertOggVorbisAbrCbrValues();
                chkOggVorbisFinerQuality.Enabled = false;
                lblOggVorbisInfo.Visible = false;
            }
        }

        private void rbtOggVorbisCbr_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtOggVorbisCbr.Checked)
            {
                trbOggVorbisBitRate.Minimum = 0;
                trbOggVorbisBitRate.Maximum = 11;
                trbOggVorbisBitRate.TickFrequency = 1;
                trbOggVorbisBitRate.Value = AudioConverterSettings.Default.trbOggVorbisBitRateCbr_Value;
                ConvertOggVorbisAbrCbrValues();
                chkOggVorbisFinerQuality.Enabled = false;
                lblOggVorbisInfo.Visible = false;
            }
        }

        private void trbOggVorbisBitRate_ValueChanged(object sender, EventArgs e)
        {
            if (rbtOggVorbisCbr.Checked)
            {
                ConvertOggVorbisAbrCbrValues();
                AudioConverterSettings.Default.trbOggVorbisBitRateCbr_Value = trbOggVorbisBitRate.Value;
            }

            if (rbtOggVorbisAbr.Checked)
            {
                ConvertOggVorbisAbrCbrValues();
                AudioConverterSettings.Default.trbOggVorbisBitRateAbr_Value = trbOggVorbisBitRate.Value;
            }

            if (rbtOggVorbisVbr.Checked)
            {
                if (chkOggVorbisFinerQuality.Checked)
                {
                    ConvertOggVorbisVbrFinerValues();
                    AudioConverterSettings.Default.trbOggVorbisBitRateVbrFiner_Value = trbOggVorbisBitRate.Value;
                }
                else
                {
                    ConvertOggVorbisVbrValues();
                    AudioConverterSettings.Default.trbOggVorbisBitRateVbr_Value = trbOggVorbisBitRate.Value;
                }
            }
        }

        private void chkOggVorbisFinerQuality_CheckedChanged(object sender, EventArgs e)
        {
            if (chkOggVorbisFinerQuality.Checked)
            {
                trbOggVorbisBitRate.Minimum = 0;
                trbOggVorbisBitRate.Maximum = 44;
                trbOggVorbisBitRate.Value = AudioConverterSettings.Default.trbOggVorbisBitRateVbr_Value * 4;
            }
            else
            {
                trbOggVorbisBitRate.Minimum = 0;
                trbOggVorbisBitRate.Maximum = 11;
                trbOggVorbisBitRate.Value = (int)Math.Round(AudioConverterSettings.Default.trbOggVorbisBitRateVbrFiner_Value / 4.0, 0, MidpointRounding.ToEven);
            }
        }

        private void cboOggVorbisFrequency_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboOggVorbisFrequency.Text)
            {
                case "[as source]":
                    _oggVorbisFrequency = string.Empty;
                    break;
                case "8 KHz":
                    _oggVorbisFrequency = string.Format("--resample=\"{0}\"", 8000);
                    break;
                case "11 KHz":
                    _oggVorbisFrequency = string.Format("--resample=\"{0}\"", 11025);
                    break;
                case "12 KHz":
                    _oggVorbisFrequency = string.Format("--resample=\"{0}\"", 12000);
                    break;
                case "16 KHz":
                    _oggVorbisFrequency = string.Format("--resample=\"{0}\"", 16000);
                    break;
                case "22 KHz":
                    _oggVorbisFrequency = string.Format("--resample=\"{0}\"", 22050);
                    break;
                case "24 KHz":
                    _oggVorbisFrequency = string.Format("--resample=\"{0}\"", 24000);
                    break;
                case "32 KHz":
                    _oggVorbisFrequency = string.Format("--resample=\"{0}\"", 32000);
                    break;
                case "44,1 KHz (CD)":
                    _oggVorbisFrequency = string.Format("--resample=\"{0}\"", 44100);
                    break;
                case "48 KHz (DAT)":
                    _oggVorbisFrequency = string.Format("--resample=\"{0}\"", 48000);
                    break;
                default:
                    _oggVorbisFrequency = string.Empty;
                    break;
            }
        }

        private void cboOggVorbisChannels_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboOggVorbisChannels.Text)
            {
                case "[as source]":
                    _oggVorbisChannels = string.Empty;
                    break;
                case "1 'mono'":
                    _oggVorbisChannels = string.Format("--downmix");
                    break;
                case "2 'stereo' (CD)":
                    _oggVorbisChannels = string.Format("--downmix=\"2\"");
                    break;
                default:
                    _oggVorbisChannels = string.Empty;
                    break;
            }
        }

        #endregion

        #region Panel Wave Events.

        private void rbtWaveUncompressed_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtWaveUncompressed.Checked)
            {
                cboWaveBitDepht.Enabled = true;
                cboWaveSampleRate.Enabled = true;
                cboWaveChannels.Enabled = true;
                btnWaveCompressedSettings.Enabled = false;
                btnWaveMp3Setings.Enabled = false;
                _waveCompression = string.Format("-compression=\"PCM\"");
            }
        }

        private void rbtWaveCompressed_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtWaveCompressed.Checked)
            {
                cboWaveBitDepht.Enabled = false;
                cboWaveSampleRate.Enabled = false;
                cboWaveChannels.Enabled = false;
                btnWaveCompressedSettings.Enabled = true;
                btnWaveMp3Setings.Enabled = false;
                _waveCompression = string.Format("-compression=\"ACM\"");
            }
        }

        private void rbtWave_mp3_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtWave_mp3.Checked)
            {
                cboWaveBitDepht.Enabled = false;
                cboWaveSampleRate.Enabled = false;
                cboWaveChannels.Enabled = false;
                btnWaveCompressedSettings.Enabled = false;
                btnWaveMp3Setings.Enabled = true;
                _waveCompression = string.Format("-compression=\"WAVEMP3\"");
            }
        }

        private void cboWaveBitDepht_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboAiffBitDepht.Text)
            {
                case "[as source]":
                    _waveBits = string.Empty;
                    break;
                case "8 bit":
                    _waveBits = string.Format("-bits=\"8\"");
                    break;
                case "16 bit (CD)":
                    _waveBits = string.Format("-bits=\"16\"");
                    break;
                case "24 bit (DVD)":
                    _waveBits = string.Format("-bits=\"24\"");
                    break;
                case "32 bit":
                    _waveBits = string.Format("-bits=\"32\"");
                    break;
                case "32 bit IEEE float":
                    _waveBits = string.Format("-bits=\"float\"");
                    break;
                default:
                    _aiffBits = string.Empty;
                    break;
            }
        }

        private void cboWaveSampleRate_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboAiffSampleRate.Text)
            {
                case "[as source]":
                    _waveFrequency = string.Empty;
                    break;
                case "8 KHz":
                    _waveFrequency = string.Format("-freq=\"8000\"");
                    break;
                case "11 KHz":
                    _waveFrequency = string.Format("-freq=\"11000\"");
                    break;
                case "12 KHz":
                    _waveFrequency = string.Format("-freq=\"12000\"");
                    break;
                case "16 KHz":
                    _waveFrequency = string.Format("-freq=\"16000\"");
                    break;
                case "22 KHz":
                    _waveFrequency = string.Format("-freq=\"22000\"");
                    break;
                case "24 KHz":
                    _waveFrequency = string.Format("-freq=\"24000\"");
                    break;
                case "32 KHz":
                    _waveFrequency = string.Format("-freq=\"32000\"");
                    break;
                case "44,1 KHz (CD)":
                    _waveFrequency = string.Format("-freq=\"44100\"");
                    break;
                case "48 KHz (DAT)":
                    _waveFrequency = string.Format("-freq=\"48000\"");
                    break;
                case "96 KHz (DVD)":
                    _waveFrequency = string.Format("-freq=\"96000\"");
                    break;
                case "192 KHz":
                    _waveFrequency = string.Format("-freq=\"192000\"");
                    break;
                default:
                    _waveFrequency = string.Empty;
                    break;
            }
        }

        private void cboWaveChannels_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboAiffChannels.Text)
            {
                case "[as source]":
                    _waveChannels = string.Empty;
                    break;
                case "1 'mono'":
                    _waveChannels = string.Format("-channels=\"1\"");
                    break;
                case "2 'stereo'":
                    _waveChannels = string.Format("-channels=\"2\"");
                    break;
                case "3":
                    _waveChannels = string.Format("-channels=\"3\"");
                    break;
                case "4 'quadraphonic'":
                    _waveChannels = string.Format("-channels=\"4\"");
                    break;
                case "5 'surround'":
                    _waveChannels = string.Format("-channels=\"5\"");
                    break;
                case "6 '5.1 surround' (DVD)":
                    _waveChannels = string.Format("-channels=\"6\"");
                    break;
                case "7":
                    _waveChannels = string.Format("-channels=\"7\"");
                    break;
                case "8 '7.1 theater'":
                    _waveChannels = string.Format("-channels=\"8\"");
                    break;
            }
        }

        private void btnWaveCompressedSettings_Click(object sender, EventArgs e)
        {
            var frm = new FormWaveCompression();

            DialogResult result = frm.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                _waveAcm = frm.WaveCompression;
            }
        }

        private void btnWaveMp3Setings_Click(object sender, EventArgs e)
        {
            var frm = new FormWaveMp3Settings();

            DialogResult result = frm.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                _waveMp3Bitrate = frm.WaveMp3Bitrate;
                _waveMp3Encoding = frm.WaveMp3Encoding;
                _waveMp3Frequency = frm.WaveMp3Frequency;
                _waveMp3Channels = frm.WaveMp3Channels;
                _waveMp3Copyright = frm.WaveMp3Copyright;
                _waveMp3Original = frm.WaveMp3Original;
                _waveMp3CrcErrorProtection = frm.WaveMp3Crc;
                _waveMp3ForceIsoCompability = frm.WaveMp3ForceIso;
            }
        }

        #endregion

        #region Panel Windows Media Events.

        private void cboWMCodec_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboWMCodec.Text)
            {
                case "Windows Media Audio Voice 9":
                    rbtWMQualityVbr.Enabled = false;
                    rbtWMBitrateCbr.Enabled = true;
                    rbtWMBitrateCbr.Checked = true;
                    chkWM2PassEncoding.Enabled = false;
                    chkWMVerify.Enabled = false;
                    LoadWmBitrateCombo();
                    LoadWmFrequencyComboBox();
                    LoadWmChannelsComboBox();
                    cboWMBitrate.SelectedIndex = AudioConverterSettings.Default.cboWMBitrateAudioVoice9_SelectedIndex;
                    cboWMChannels.SelectedIndex = 0;
                    break;
                case "Windows Media Audio 9.2 Lossless":
                    rbtWMQualityVbr.Enabled = true;
                    rbtWMQualityVbr.Checked = true;
                    rbtWMBitrateCbr.Enabled = false;
                    chkWM2PassEncoding.Enabled = false;
                    chkWMVerify.Enabled = true;
                    LoadWmFrequencyComboBox();
                    LoadWmChannelsComboBox();
                    LoadWmBitrateCombo();
                    cboWMFrequency.SelectedIndex = AudioConverterSettings.Default.cboWMFrequencyAudio92Lossless_SelectedIndex;
                    cboWMBitrate.SelectedIndex = 0;
                    break;
                case "Windows Media Audio 9.2":
                    rbtWMQualityVbr.Enabled = true;
                    rbtWMBitrateCbr.Enabled = true;
                    chkWM2PassEncoding.Enabled = true;
                    chkWMVerify.Enabled = false;
                    LoadWmFrequencyComboBox();
                    LoadWmChannelsComboBox();
                    LoadWmBitrateCombo();
                    rbtWMQualityVbr.Checked = AudioConverterSettings.Default.rbtWMQualityVbrAudio92_Checked;
                    rbtWMBitrateCbr.Checked = AudioConverterSettings.Default.rbtWMBitrateCbrAudio92_Checked;
                    chkWM2PassEncoding.Checked = AudioConverterSettings.Default.chkWM2PassEncodingAudio92_Checked;
                    if (rbtWMQualityVbr.Checked)
                    {
                        cboWMBitrate.SelectedIndex = AudioConverterSettings.Default.cboWMBitrateAudio92Vbr_SelectedIndex;
                    }
                    else
                    {
                        cboWMBitrate.SelectedIndex = AudioConverterSettings.Default.cboWMBitrateAudio92Cbr_SelectedIndex;
                    }
                    break;
                case "Windows Media Audio 10 Professional":
                    rbtWMQualityVbr.Enabled = true;
                    rbtWMBitrateCbr.Enabled = true;
                    chkWM2PassEncoding.Enabled = true;
                    chkWMVerify.Enabled = false;
                    LoadWmFrequencyComboBox();
                    LoadWmChannelsComboBox();
                    LoadWmBitrateCombo();
                    rbtWMQualityVbr.Checked = AudioConverterSettings.Default.rbtWMQualityVbrAudio10_Checked;
                    rbtWMBitrateCbr.Checked = AudioConverterSettings.Default.rbtWMBitrateCbrAudio10_Checked;
                    chkWM2PassEncoding.Checked = AudioConverterSettings.Default.chkWM2PassEncodingAudio10_Checked;
                    if (rbtWMQualityVbr.Checked)
                    {
                        cboWMBitrate.SelectedIndex = AudioConverterSettings.Default.cboWMBitrateAudio10Vbr_SelectedIndex;
                    }
                    else
                    {
                        cboWMBitrate.SelectedIndex = AudioConverterSettings.Default.cboWMBitrateAudio10Cbr_SelectedIndex;
                    }
                    break;
            }
        }

        private void rbtWMQualityVbr_CheckedChanged(object sender, EventArgs e)
        {
            if (cboWMCodec.Text == @"Windows Media Audio 9.2")
            {
                LoadWmFrequencyComboBox();
                LoadWmChannelsComboBox();
                LoadWmBitrateCombo();
                if (rbtWMQualityVbr.Checked)
                {
                    cboWMBitrate.SelectedIndex = AudioConverterSettings.Default.cboWMBitrateAudio92Vbr_SelectedIndex;
                }
                else
                {
                    cboWMBitrate.SelectedIndex = AudioConverterSettings.Default.cboWMBitrateAudio92Cbr_SelectedIndex;
                }
                AudioConverterSettings.Default.rbtWMQualityVbrAudio92_Checked = rbtWMQualityVbr.Checked;
            }

            if (cboWMCodec.Text == @"Windows Media Audio 10 Professional")
            {
                LoadWmFrequencyComboBox();
                LoadWmChannelsComboBox();
                LoadWmBitrateCombo();
                if (rbtWMQualityVbr.Checked)
                {
                    cboWMBitrate.SelectedIndex = AudioConverterSettings.Default.cboWMBitrateAudio10Vbr_SelectedIndex;
                }
                else
                {
                    cboWMBitrate.SelectedIndex = AudioConverterSettings.Default.cboWMBitrateAudio10Cbr_SelectedIndex;
                }
                AudioConverterSettings.Default.rbtWMQualityVbrAudio10_Checked = rbtWMQualityVbr.Checked;
            }
        }

        private void rbtWMBitrateCbr_CheckedChanged(object sender, EventArgs e)
        {
            if (cboWMCodec.Text == @"Windows Media Audio 9.2")
            {
                LoadWmFrequencyComboBox();
                LoadWmChannelsComboBox();
                LoadWmBitrateCombo();
                if (rbtWMQualityVbr.Checked)
                {
                    cboWMBitrate.SelectedIndex = AudioConverterSettings.Default.cboWMBitrateAudio92Vbr_SelectedIndex;
                }
                else
                {
                    cboWMBitrate.SelectedIndex = AudioConverterSettings.Default.cboWMBitrateAudio92Cbr_SelectedIndex;
                }
                AudioConverterSettings.Default.rbtWMBitrateCbrAudio92_Checked = rbtWMBitrateCbr.Checked;
            }

            if (cboWMCodec.Text == @"Windows Media Audio 10 Professional")
            {
                LoadWmFrequencyComboBox();
                LoadWmChannelsComboBox();
                LoadWmBitrateCombo();
                if (rbtWMQualityVbr.Checked)
                {
                    cboWMBitrate.SelectedIndex = AudioConverterSettings.Default.cboWMBitrateAudio10Vbr_SelectedIndex;
                }
                else
                {
                    cboWMBitrate.SelectedIndex = AudioConverterSettings.Default.cboWMBitrateAudio10Cbr_SelectedIndex;
                }
                AudioConverterSettings.Default.rbtWMBitrateCbrAudio10_Checked = rbtWMBitrateCbr.Checked;
            }
        }

        private void chkWM2PassEncoding_CheckedChanged(object sender, EventArgs e)
        {
            if (cboWMCodec.Text == @"Windows Media Audio 9.2")
            {
                LoadWmFrequencyComboBox();
                LoadWmChannelsComboBox();
                LoadWmBitrateCombo();
                if (rbtWMQualityVbr.Checked)
                {
                    cboWMBitrate.SelectedIndex = 3;
                }
                else
                {
                    cboWMBitrate.SelectedIndex = 3;
                }

                AudioConverterSettings.Default.chkWM2PassEncodingAudio92_Checked = chkWM2PassEncoding.Checked;
            }

            if (cboWMCodec.Text == @"Windows Media Audio 10 Professional")
            {
                LoadWmFrequencyComboBox();
                LoadWmChannelsComboBox();
                LoadWmBitrateCombo();
                if (rbtWMQualityVbr.Checked)
                {
                    cboWMBitrate.SelectedIndex = 0;
                }
                else
                {
                    cboWMBitrate.SelectedIndex = 0;
                }

                AudioConverterSettings.Default.chkWM2PassEncodingAudio10_Checked = chkWM2PassEncoding.Checked;
            }
        }

        private void cboWMBitrate_SelectedIndexChanged(object sender, EventArgs e)
        {
            cboWMFrequency.SelectedIndexChanged -= cboWMFrequency_SelectedIndexChanged;
            cboWMChannels.SelectedIndexChanged -= cboWMChannels_SelectedIndexChanged;

            switch (cboWMCodec.Text)
            {
                case "Windows Media Audio Voice 9":
                    BitrateAudioVoice9Changed();
                    AudioConverterSettings.Default.cboWMBitrateAudioVoice9_SelectedIndex = cboWMBitrate.SelectedIndex;
                    break;
                case "Windows Media Audio 9.2 Lossless":
                    break;
                case "Windows Media Audio 9.2":
                    if (rbtWMBitrateCbr.Checked)
                    {
                        AudioConverterSettings.Default.cboWMBitrateAudio92Cbr_SelectedIndex = cboWMBitrate.SelectedIndex;
                    }
                    
                    if (rbtWMQualityVbr.Checked)
                    {
                        AudioConverterSettings.Default.cboWMBitrateAudio92Vbr_SelectedIndex = cboWMBitrate.SelectedIndex;
                    }
                    
                    BitrateAudio92Changed();
                    break;
                case "Windows Media Audio 10 Professional":
                    if (rbtWMBitrateCbr.Checked)
                    {
                        AudioConverterSettings.Default.cboWMBitrateAudio10Cbr_SelectedIndex = cboWMBitrate.SelectedIndex;
                    }
                    
                    if (rbtWMQualityVbr.Checked)
                    {
                        AudioConverterSettings.Default.cboWMBitrateAudio10Vbr_SelectedIndex = cboWMBitrate.SelectedIndex;
                    }
                    BitrateAudio10Changed();
                    break;
            }

            cboWMChannels.SelectedIndexChanged += cboWMChannels_SelectedIndexChanged;
            cboWMFrequency.SelectedIndexChanged += cboWMFrequency_SelectedIndexChanged;
        }

        private void cboWMFrequency_SelectedIndexChanged(object sender, EventArgs e)
        {
            cboWMBitrate.SelectedIndexChanged -= cboWMBitrate_SelectedIndexChanged;
            cboWMChannels.SelectedIndexChanged -= cboWMChannels_SelectedIndexChanged;

            switch (cboWMCodec.Text)
            {
                case "Windows Media Audio Voice 9":
                    FrequencyAudioVoice9Changed();
                    break;
                case "Windows Media Audio 9.2 Lossless":
                    FrequencyAudio92LosslessChanged();
                    AudioConverterSettings.Default.cboWMFrequencyAudio92Lossless_SelectedIndex = cboWMFrequency.SelectedIndex;
                    break;
                case "Windows Media Audio 9.2":
                    FrequencyAudio92Changed();
                    break;
                case "Windows Media Audio 10 Professional":
                    FrequencyAudio10Changed();
                    break;
            }

            cboWMChannels.SelectedIndexChanged += cboWMChannels_SelectedIndexChanged;
            cboWMBitrate.SelectedIndexChanged += cboWMBitrate_SelectedIndexChanged;
        }

        private void cboWMChannels_SelectedIndexChanged(object sender, EventArgs e)
        {
            cboWMBitrate.SelectedIndexChanged -= cboWMBitrate_SelectedIndexChanged;
            cboWMFrequency.SelectedIndexChanged -= cboWMFrequency_SelectedIndexChanged;

            switch (cboWMCodec.Text)
            {
                case "Windows Media Audio Voice 9":
                    break;
                case "Windows Media Audio 9.2 Lossless":
                    ChannelsAudio92LosslessChanged();
                    break;
                case "Windows Media Audio 9.2":
                    ChannelsAudio92Changed();
                    break;
                case "Windows Media Audio 10 Professional":
                    ChannelsAudio10Changed();
                    break;
            }

            cboWMBitrate.SelectedIndexChanged += cboWMBitrate_SelectedIndexChanged;
            cboWMFrequency.SelectedIndexChanged += cboWMFrequency_SelectedIndexChanged;
        }

        #region Load Combos.

        private void LoadWmBitrateCombo()
        {
            cboWMBitrate.Items.Clear();
            switch (cboWMCodec.Text)
            {
                case "Windows Media Audio Voice 9":
                    cboWMBitrate.Items.Add(new ComboBoxItem("4 kbps", "4 kbps", Color.Black));
                    cboWMBitrate.Items.Add(new ComboBoxItem("5 kbps", "5 kbps", Color.Black));
                    cboWMBitrate.Items.Add(new ComboBoxItem("8 kbps", "8 kbps", Color.Black));
                    cboWMBitrate.Items.Add(new ComboBoxItem("10 kbps", "10 kbps", Color.Black));
                    cboWMBitrate.Items.Add(new ComboBoxItem("12 kbps", "12 kbps", Color.Black));
                    cboWMBitrate.Items.Add(new ComboBoxItem("16 kbps", "16 kbps", Color.Black));
                    cboWMBitrate.Items.Add(new ComboBoxItem("20 kbps", "20 kbps", Color.Black));
                    break;
                case "Windows Media Audio 9.2 Lossless":
                    cboWMBitrate.Items.Add(new ComboBoxItem("VBR Quality 100", "VBR Quality 100", Color.Black));
                    break;
                case "Windows Media Audio 9.2":
                    if (rbtWMQualityVbr.Checked)
                    {
                        if (chkWM2PassEncoding.Checked)
                        {
                            cboWMBitrate.Items.Add(new ComboBoxItem("48 kbps", "48 kbps", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("64 kbps", "64 kbps", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("96 kbps", "96 kbps", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("128 kbps", "128 kbps", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("160 kbps", "160 kbps", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("192 kbps", "192 kbps", Color.Black));
                        }
                        else
                        {
                            cboWMBitrate.Items.Add(new ComboBoxItem("VBR Quality 10", "VBR Quality 10", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("VBR Quality 25", "VBR Quality 25", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("VBR Quality 50", "VBR Quality 50", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("VBR Quality 75", "VBR Quality 75", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("VBR Quality 90", "VBR Quality 90", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("VBR Quality 98", "VBR Quality 98", Color.Black));
                        }
                    }
                    else
                    {
                        cboWMBitrate.Items.Add(new ComboBoxItem("0 kbps", "0 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("5 kbps", "5 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("6 kbps", "6 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("8 kbps", "8 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("10 kbps", "10 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("12 kbps", "12 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("16 kbps", "16 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("20 kbps", "20 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("22 kbps", "22 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("24 kbps", "24 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("32 kbps", "32 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("40 kbps", "40 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("48 kbps", "48 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("63 kbps", "63 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("64 kbps", "64 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("80 kbps", "80 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("95 kbps", "95 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("96 kbps", "96 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("127 kbps", "127 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("128 kbps", "128 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("160 kbps", "160 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("191 kbps", "191 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("192 kbps", "192 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("256 kbps", "256 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("320 kbps", "320 kbps", Color.Black));
                    }
                    break;
                case "Windows Media Audio 10 Professional":
                    if (rbtWMQualityVbr.Checked)
                    {
                        if (chkWM2PassEncoding.Checked)
                        {
                            cboWMBitrate.Items.Add(new ComboBoxItem("48 kbps", "48 kbps", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("64 kbps", "64 kbps", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("128 kbps", "128 kbps", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("192 kbps", "192 kbps", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("256 kbps", "256 kbps", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("384 kbps", "384 kbps", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("440 kbps", "440 kbps", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("640 kbps", "640 kbps", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("768 kbps", "768 kbps", Color.Black));
                        }
                        else
                        {
                            cboWMBitrate.Items.Add(new ComboBoxItem("VBR Quality 10", "VBR Quality 10", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("VBR Quality 25", "VBR Quality 25", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("VBR Quality 50", "VBR Quality 50", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("VBR Quality 75", "VBR Quality 75", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("VBR Quality 90", "VBR Quality 90", Color.Black));
                            cboWMBitrate.Items.Add(new ComboBoxItem("VBR Quality 98", "VBR Quality 98", Color.Black));
                        }
                    }
                    else
                    {
                        cboWMBitrate.Items.Add(new ComboBoxItem("32 kbps", "32 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("48 kbps", "48 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("64 kbps", "64 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("80 kbps", "80 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("96 kbps", "96 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("127 kbps", "127 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("128 kbps", "128 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("160 kbps", "160 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("191 kbps", "191 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("192 kbps", "192 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("255 kbps", "255 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("256 kbps", "256 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("383 kbps", "383 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("384 kbps", "384 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("440 kbps", "440 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("640 kbps", "640 kbps", Color.Black));
                        cboWMBitrate.Items.Add(new ComboBoxItem("768 kbps", "768 kbps", Color.Black));
                    }
                    break;
            }
        }

        private void LoadWmFrequencyComboBox()
        {
            cboWMFrequency.Items.Clear();
            switch (cboWMCodec.Text)
            {
                case "Windows Media Audio Voice 9":
                    cboWMFrequency.Items.Add(new ComboBoxItem("8 kHz", "8 kHz", Color.Black));
                    cboWMFrequency.Items.Add(new ComboBoxItem("11.025 kHz", "11.025 kHz", Color.Black));
                    cboWMFrequency.Items.Add(new ComboBoxItem("16 kHz", "16 kHz", Color.Black));
                    cboWMFrequency.Items.Add(new ComboBoxItem("22.05 kHz", "22.05 kHz", Color.Black));
                    break;
                case "Windows Media Audio 9.2 Lossless":
                    cboWMFrequency.Items.Add(new ComboBoxItem("44 kHz", "44 kHz", Color.Black));
                    cboWMFrequency.Items.Add(new ComboBoxItem("48 kHz", "48 kHz", Color.Black));
                    cboWMFrequency.Items.Add(new ComboBoxItem("88 kHz", "88 kHz", Color.Black));
                    cboWMFrequency.Items.Add(new ComboBoxItem("96 kHz", "96 kHz", Color.Black));
                    break;
                case "Windows Media Audio 9.2":
                    if (rbtWMQualityVbr.Checked)
                    {
                        cboWMFrequency.Items.Add(new ComboBoxItem("44 kHz", "44 kHz", Color.Black));
                        cboWMFrequency.Items.Add(new ComboBoxItem("48 kHz", "48 kHz", Color.Black));
                    }
                    else
                    {
                        cboWMFrequency.Items.Add(new ComboBoxItem("8 kHz", "8 kHz", Color.Black));
                        cboWMFrequency.Items.Add(new ComboBoxItem("11 kHz", "11 kHz", Color.Black));
                        cboWMFrequency.Items.Add(new ComboBoxItem("16 kHz", "16 kHz", Color.Black));
                        cboWMFrequency.Items.Add(new ComboBoxItem("22 kHz", "22 kHz", Color.Black));
                        cboWMFrequency.Items.Add(new ComboBoxItem("32 kHz", "32 kHz", Color.Black));
                        cboWMFrequency.Items.Add(new ComboBoxItem("44 kHz", "44 kHz", Color.Black));
                        cboWMFrequency.Items.Add(new ComboBoxItem("48 kHz", "48 kHz", Color.Black));
                    }
                    break;
                case "Windows Media Audio 10 Professional":
                    if (rbtWMQualityVbr.Checked)
                    {
                        cboWMFrequency.Items.Add(new ComboBoxItem("44 kHz", "44 kHz", Color.Black));
                        cboWMFrequency.Items.Add(new ComboBoxItem("48 kHz", "48 kHz", Color.Black));
                        cboWMFrequency.Items.Add(new ComboBoxItem("88 kHz", "88 kHz", Color.Black));
                        cboWMFrequency.Items.Add(new ComboBoxItem("96 kHz", "96 kHz", Color.Black));
                    }
                    else
                    {
                        cboWMFrequency.Items.Add(new ComboBoxItem("32 kHz", "32 kHz", Color.Black));
                        cboWMFrequency.Items.Add(new ComboBoxItem("44 kHz", "44 kHz", Color.Black));
                        cboWMFrequency.Items.Add(new ComboBoxItem("48 kHz", "48 kHz", Color.Black));
                        cboWMFrequency.Items.Add(new ComboBoxItem("88 kHz", "88 kHz", Color.Black));
                        cboWMFrequency.Items.Add(new ComboBoxItem("96 kHz", "96 kHz", Color.Black));
                    }
                    break;
            }
        }

        private void LoadWmChannelsComboBox()
        {
            cboWMChannels.Items.Clear();
            switch (cboWMCodec.Text)
            {
                case "Windows Media Audio Voice 9":
                    cboWMChannels.Items.Add(new ComboBoxItem("mono", "mono", Color.Black));
                    break;
                case "Windows Media Audio 9.2 Lossless":
                    cboWMChannels.Items.Add(new ComboBoxItem("2 channel 16 bit VBR", "2 channel 16 bit VBR", Color.Black));
                    cboWMChannels.Items.Add(new ComboBoxItem("2 channel 24 bit VBR", "2 channel 24 bit VBR", Color.Black));
                    cboWMChannels.Items.Add(new ComboBoxItem("5.1 channel 24 bit VBR", "5.1 channel 24 bit VBR", Color.Black));
                    break;
                case "Windows Media Audio 9.2":
                    if (rbtWMQualityVbr.Checked)
                    {
                        cboWMChannels.Items.Add(new ComboBoxItem("stereo VBR", "stereo VBR", Color.Black));
                    }
                    else
                    {
                        cboWMChannels.Items.Add(new ComboBoxItem("mono CBR", "mono CBR", Color.Black));
                        cboWMChannels.Items.Add(new ComboBoxItem("stereo CBR", "stereo CBR", Color.Black));
                        cboWMChannels.Items.Add(new ComboBoxItem("stereo Low Delay CBR", "stereo Low Delay CBR", Color.Black));
                    }
                    break;
                case "Windows Media Audio 10 Professional":
                    if (rbtWMQualityVbr.Checked)
                    {
                        cboWMChannels.Items.Add(new ComboBoxItem("2 channel 16 bit VBR", "2 channel 16 bit VBR", Color.Black));
                        cboWMChannels.Items.Add(new ComboBoxItem("2 channel 24 bit VBR", "2 channel 24 bit VBR", Color.Black));
                        cboWMChannels.Items.Add(new ComboBoxItem("5.1 channel 16 bit VBR", "5.1 channel 16 bit VBR", Color.Black));
                        cboWMChannels.Items.Add(new ComboBoxItem("5.1 channel 24 bit VBR", "5.1 channel 24 bit VBR", Color.Black));
                    }
                    else
                    {
                        cboWMChannels.Items.Add(new ComboBoxItem("2 channel 16 bit CBR", "2 channel 16 bit CBR", Color.Black));
                        cboWMChannels.Items.Add(new ComboBoxItem("2 channel 24 bit CBR", "2 channel 24 bit CBR", Color.Black));
                        cboWMChannels.Items.Add(new ComboBoxItem("2 channel 24 bit Low Delay CBR", "2 channel 24 bit Low Delay CBR", Color.Black));
                        cboWMChannels.Items.Add(new ComboBoxItem("5.1 channel 16 bit CBR", "5.1 channel 16 bit CBR", Color.Black));
                        cboWMChannels.Items.Add(new ComboBoxItem("5.1 channel 24 bit CBR", "5.1 channel 24 bit CBR", Color.Black));
                        cboWMChannels.Items.Add(new ComboBoxItem("5.1 channel 24 bit Low Delay CBR", "5.1 channel 24 bit Low Delay CBR", Color.Black));
                        cboWMChannels.Items.Add(new ComboBoxItem("7.1 channel 16 bit CBR", "7.1 channel 16 bit CBR", Color.Black));
                        cboWMChannels.Items.Add(new ComboBoxItem("7.1 channel 24 bit CBR", "7.1 channel 24 bit CBR", Color.Black));
                    }
                    break;
            }
        }

        #endregion

        #region Configure Audio Voice 9

        private void BitrateAudioVoice9Changed()
        {
            FormatWmBitrateCombo(true);

            switch (cboWMBitrate.Text)
            {
                case "4 kbps":
                case "5 kbps":
                case "8 kbps":
                    FormatWMFrequencyCombo(false, 1, 2, 3);
                    cboWMFrequency.SelectedIndex = 0;
                    break;
                case "10 kbps":
                    FormatWMFrequencyCombo(false, 0, 2, 3);
                    cboWMFrequency.SelectedIndex = 1;
                    break;
                case "12 kbps":
                case "16 kbps":
                    FormatWMFrequencyCombo(false, 0, 1, 3);
                    cboWMFrequency.SelectedIndex = 2;
                    break;
                case "20 kbps":
                    FormatWMFrequencyCombo(false, 0, 1, 2);
                    cboWMFrequency.SelectedIndex = 3;
                    break;
            }
        }

        private void FrequencyAudioVoice9Changed()
        {
            FormatWMFrequencyCombo(true);

            switch (cboWMFrequency.Text)
            {
                case "8 kHz":
                    FormatWmBitrateCombo(false, 3, 4, 5, 6);
                    cboWMBitrate.SelectedIndex = 0;
                    break;
                case "11.025 kHz":
                    FormatWmBitrateCombo(false, 0, 1, 2, 4, 5, 6);
                    cboWMBitrate.SelectedIndex = 3;
                    break;
                case "16 kHz":
                    FormatWmBitrateCombo(false, 0, 1, 2, 3, 6);
                    cboWMBitrate.SelectedIndex = 4;
                    break;
                case "22.05 kHz":
                    FormatWmBitrateCombo(false, 0, 1, 2, 3, 4, 5);
                    cboWMBitrate.SelectedIndex = 6;
                    break;
            }
        }

        #endregion

        #region Configure  Audio 9.2 Lossless.

        private void FrequencyAudio92LosslessChanged()
        {
            FormatWMFrequencyCombo(true);

            switch (cboWMFrequency.Text)
            {
                case "44 kHz":
                    FormatWMChannelsCombo(false, 2);
                    cboWMChannels.SelectedIndex = 0;
                    break;
                case "48 kHz":
                    FormatWMChannelsCombo(false, 0);
                    cboWMChannels.SelectedIndex = 1;
                    break;
                case "88 kHz":
                    FormatWMChannelsCombo(false, 0);
                    cboWMChannels.SelectedIndex = 1;
                    break;
                case "96 kHz":
                    FormatWMChannelsCombo(false, 0);
                    cboWMChannels.SelectedIndex = 1;
                    break;
            }
        }

        private void ChannelsAudio92LosslessChanged()
        {
            FormatWMChannelsCombo(true);

            switch (cboWMChannels.Text)
            {
                case "2 channel 16 bit VBR":
                    FormatWMFrequencyCombo(false, 1, 2, 3);
                    cboWMFrequency.SelectedIndex = 0;
                    break;
                case "2 channel 24 bit VBR":
                    FormatWMFrequencyCombo(true);
                    break;
                case "5.1 channel 24 bit VBR":
                    FormatWMFrequencyCombo(false, 0);
                    cboWMFrequency.SelectedIndex = 1;
                    break;
            }
        }

        #endregion

        #region Configure Audio 9.2

        private void BitrateAudio92Changed()
        {
            FormatWmBitrateCombo(true);

            if (rbtWMQualityVbr.Checked)
            {
                if (chkWM2PassEncoding.Checked)
                {
                    switch (cboWMBitrate.Text)
                    {
                        case "48 kbps":
                        case "64 kbps":
                        case "96 kbps":
                        case "128 kbps":
                        case "160 kbps":
                            FormatWMFrequencyCombo(false, 1);
                            FormatWMChannelsCombo(true);
                            cboWMFrequency.SelectedIndex = 0;
                            cboWMChannels.SelectedIndex = 0;
                            break;
                        case "192 kbps":
                            FormatWMFrequencyCombo(false, 1);
                            FormatWMChannelsCombo(true);
                            cboWMFrequency.SelectedIndex = 0;
                            break;
                    }
                }
                else
                {
                    switch (cboWMBitrate.Text)
                    {
                        case "VBR Quality 10":
                        case "VBR Quality 25":
                        case "VBR Quality 50":
                        case "VBR Quality 75":
                            FormatWMFrequencyCombo(false, 1);
                            FormatWMChannelsCombo(true);
                            cboWMFrequency.SelectedIndex = 0;
                            break;
                        case "VBR Quality 90":
                        case "VBR Quality 98":
                            FormatWMFrequencyCombo(false, 1);
                            FormatWMChannelsCombo(true);
                            cboWMFrequency.SelectedIndex = 0;
                            break;
                    }
                }
            }
            
            if(rbtWMBitrateCbr.Checked)
            {
                switch(cboWMBitrate.Text)
                {
                    case "0 kbps":
                    case "5 kbps":
                    case "6 kbps":
                        FormatWMFrequencyCombo(false, 1, 2, 3, 4, 5, 6);
                        FormatWMChannelsCombo(false, 1, 2);
                        cboWMFrequency.SelectedIndex = 0;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "8 kbps":
                        FormatWMFrequencyCombo(false, 2, 3, 4, 5, 6);
                        FormatWMChannelsCombo(false, 1, 2);
                        cboWMFrequency.SelectedIndex = 0;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "10 kbps":
                        FormatWMFrequencyCombo(false, 0, 3, 4, 5, 6);
                        FormatWMChannelsCombo(false, 1, 2);
                        cboWMFrequency.SelectedIndex = 1;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "12 kbps":
                        FormatWMFrequencyCombo(false, 1, 3, 4, 5, 6);
                        FormatWMChannelsCombo(false, 2);
                        cboWMFrequency.SelectedIndex = 0;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "16 kbps":
                        FormatWMFrequencyCombo(false, 0, 1, 4, 5, 6);
                        FormatWMChannelsCombo(false, 2);
                        cboWMFrequency.SelectedIndex = 2;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "20 kbps":
                        FormatWMFrequencyCombo(false, 0, 1, 6);
                        FormatWMChannelsCombo(false, 2);
                        cboWMFrequency.SelectedIndex = 2;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "22 kbps":
                        FormatWMFrequencyCombo(false, 0, 1, 2, 4, 5, 6);
                        FormatWMChannelsCombo(false, 0, 2);
                        cboWMFrequency.SelectedIndex = 3;
                        cboWMChannels.SelectedIndex = 1;
                        break;
                    case "24 kbps":
                        FormatWMFrequencyCombo(false, 0, 1, 2, 3, 5, 6);
                        FormatWMChannelsCombo(false, 0, 2);
                        cboWMFrequency.SelectedIndex = 4;
                        cboWMChannels.SelectedIndex = 1;
                        break;
                    case "32 kbps":
                        FormatWMFrequencyCombo(false, 0, 1, 2, 6);
                        FormatWMChannelsCombo(false, 2);
                        cboWMFrequency.SelectedIndex = 3;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "40 kbps":
                        FormatWMFrequencyCombo(false, 0, 1, 2, 3, 5, 6);
                        FormatWMChannelsCombo(false, 0, 2);
                        cboWMFrequency.SelectedIndex = 4;
                        cboWMChannels.SelectedIndex = 1;
                        break;
                    case "48 kbps":
                        FormatWMFrequencyCombo(false, 0, 1, 2, 3, 6);
                        FormatWMChannelsCombo(false, 2);
                        cboWMFrequency.SelectedIndex = 4;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "63 kbps":
                    case "95 kbps":
                    case "127 kbps":
                    case "191 kbps":
                        FormatWMFrequencyCombo(false, 0, 1, 2, 3, 4, 5);
                        FormatWMChannelsCombo(false, 0, 1);
                        cboWMFrequency.SelectedIndex = 6;
                        cboWMChannels.SelectedIndex = 2;
                        break;
                    case "64 kbps":
                    case "96 kbps":
                    case "128 kbps":
                    case "160 kbps":
                    case "192 kbps":
                        FormatWMFrequencyCombo(false, 0, 1, 2, 3, 4);
                        FormatWMChannelsCombo(false, 0, 2);
                        cboWMFrequency.SelectedIndex = 5;
                        cboWMChannels.SelectedIndex = 1;
                        break;
                    case "80 kbps":
                    case "256 kbps":
                    case "320 kbps":
                        FormatWMFrequencyCombo(false, 0, 1, 2, 3, 4, 6);
                        FormatWMChannelsCombo(false, 0, 2);
                        cboWMFrequency.SelectedIndex = 5;
                        cboWMChannels.SelectedIndex = 1;
                        break;
                }
            }
        }

        private void FrequencyAudio92Changed()
        {
            FormatWMFrequencyCombo(true);

            if (rbtWMQualityVbr.Checked)
            {
                if (chkWM2PassEncoding.Checked)
                {
                    switch (cboWMFrequency.Text)
                    {
                        case "44 kHz":
                            FormatWmBitrateCombo(true);
                            FormatWMChannelsCombo(true);
                            break;
                        case "48 kHz":
                            FormatWmBitrateCombo(false, 0, 1, 2, 3);
                            FormatWMChannelsCombo(true);
                            cboWMBitrate.SelectedIndex = 4;
                            break;
                    }
                }
                else
                {
                    switch (cboWMFrequency.Text)
                    {
                        case "44 kHz":
                            FormatWmBitrateCombo(true);
                            FormatWMChannelsCombo(true);
                            break;
                        case "48 kHz":
                            FormatWmBitrateCombo(false, 0, 4);
                            FormatWMChannelsCombo(true);
                            cboWMBitrate.SelectedIndex = 1;
                            break;
                    }
                }
            }

            if (rbtWMBitrateCbr.Checked)
            {
                switch (cboWMFrequency.Text)
                {
                    case "8 kHz":
                        FormatWmBitrateCombo(false, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24);
                        FormatWMChannelsCombo(false, 2);
                        cboWMBitrate.SelectedIndex = 0;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "11 kHz":
                        FormatWmBitrateCombo(false, 0, 1, 2, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24);
                        FormatWMChannelsCombo(false, 1, 2);
                        cboWMBitrate.SelectedIndex = 3;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "16 kHz":
                        FormatWmBitrateCombo(false, 0, 1, 2, 3, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24);
                        FormatWMChannelsCombo(false, 2);
                        cboWMBitrate.SelectedIndex = 4;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "22 kHz":
                        FormatWmBitrateCombo(false, 0, 1, 2, 3, 4, 5, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24);
                        FormatWMChannelsCombo(false, 2);
                        cboWMBitrate.SelectedIndex = 6;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "32 kHz":
                        FormatWmBitrateCombo(false, 0, 1, 2, 3, 4, 5, 6, 8, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24);
                        FormatWMChannelsCombo(false, 2);
                        cboWMBitrate.SelectedIndex = 7;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "44 kHz":
                        FormatWmBitrateCombo(false, 0, 1, 2, 3, 4, 5, 6, 8, 9, 11, 13, 16, 18, 21);
                        FormatWMChannelsCombo(false, 2);
                        cboWMBitrate.SelectedIndex = 7;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "48 kHz":
                        FormatWmBitrateCombo(false, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 15, 23, 24);
                        FormatWMChannelsCombo(false, 0);
                        cboWMBitrate.SelectedIndex = 13;
                        cboWMChannels.SelectedIndex = 1;
                        break;
                }
            }
        }

        private void ChannelsAudio92Changed()
        {
            FormatWMChannelsCombo(true);

            if (rbtWMQualityVbr.Checked)
            {
                cboWMChannels.SelectedIndex = 0;
            }

            if (rbtWMBitrateCbr.Checked)
            {
                switch (cboWMChannels.Text)
                {
                    case "mono CBR":
                        FormatWmBitrateCombo(false, 8, 9, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24);
                        FormatWMFrequencyCombo(false, 6);
                        cboWMBitrate.SelectedIndex = 0;
                        cboWMFrequency.SelectedIndex = 0;
                        break;
                    case "stereo CBR":
                        FormatWmBitrateCombo(false, 0, 1, 2, 3, 4, 13, 16, 18, 21);
                        FormatWMFrequencyCombo(false, 1);
                        cboWMBitrate.SelectedIndex = 5;
                        cboWMFrequency.SelectedIndex = 0;
                        break;
                    case "stereo Low Delay CBR":
                        FormatWmBitrateCombo(false, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 17, 19, 20, 22, 23, 24);
                        FormatWMFrequencyCombo(false, 0, 1, 2, 3, 4, 5);
                        cboWMBitrate.SelectedIndex = 13;
                        cboWMFrequency.SelectedIndex = 6;
                        break;
                }
            }
        }

        #endregion

        #region Configure Audio 10 Professional.

        private void BitrateAudio10Changed()
        {
            FormatWmBitrateCombo(true);

            if (rbtWMQualityVbr.Checked)
            {
                if (chkWM2PassEncoding.Checked)
                {
                    switch (cboWMBitrate.Text)
                    {
                        case "48 kbps":
                        case "64 kbps":
                            FormatWMFrequencyCombo(false, 1, 2, 3);
                            FormatWMChannelsCombo(false, 1, 2, 3);
                            cboWMFrequency.SelectedIndex = 0;
                            cboWMChannels.SelectedIndex = 0;
                            break;
                        case "128 kbps":
                        case "192 kbps":
                        case "256 kbps":
                        case "384 kbps":
                        case "440 kbps":
                            FormatWMFrequencyCombo(true);
                            FormatWMChannelsCombo(false, 0);
                            cboWMChannels.SelectedIndex = 1;
                            break;
                        case "640 kbps":
                        case "768 kbps":
                            FormatWMFrequencyCombo(false, 2);
                            FormatWMChannelsCombo(false, 0, 1, 2);
                            cboWMFrequency.SelectedIndex = 0;
                            cboWMChannels.SelectedIndex = 3;
                            break;
                    }
                }
                else
                {
                    switch (cboWMBitrate.Text)
                    {
                        case "VBR Quality 10":
                        case "VBR Quality 25":
                        case "VBR Quality 50":
                            FormatWMFrequencyCombo(false, 2, 3);
                            FormatWMChannelsCombo(true);
                            cboWMFrequency.SelectedIndex = 0;
                            break;
                        case "VBR Quality 75":
                        case "VBR Quality 90":
                        case "VBR Quality 98":
                            FormatWMFrequencyCombo(true);
                            FormatWMChannelsCombo(true);
                            break;
                    }
                }
            }

            if (rbtWMBitrateCbr.Checked)
            {
                switch (cboWMBitrate.Text)
                {
                    case "32 kbps":
                        FormatWMFrequencyCombo(false, 1, 2, 3, 4);
                        FormatWMChannelsCombo(false, 1, 2, 3, 4, 5, 6, 7);
                        cboWMFrequency.SelectedIndex = 0;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "48 kbps":
                    case "64 kbps":
                    case "80 kbps":
                    case "96 kbps":
                        FormatWMFrequencyCombo(false, 0, 3, 4);
                        FormatWMChannelsCombo(false, 1, 2, 3, 4, 5, 6, 7);
                        cboWMFrequency.SelectedIndex = 1;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "127 kbps":
                        FormatWMFrequencyCombo(false, 0, 1, 3, 4);
                        FormatWMChannelsCombo(false, 0, 1, 3, 4, 5, 6, 7);
                        cboWMFrequency.SelectedIndex = 2;
                        cboWMChannels.SelectedIndex = 2;
                        break;
                    case "128 kbps":
                        FormatWMFrequencyCombo(false, 0);
                        FormatWMChannelsCombo(false, 2, 5, 7);
                        cboWMFrequency.SelectedIndex = 1;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "160 kbps":
                        FormatWMFrequencyCombo(false, 0, 3, 4);
                        FormatWMChannelsCombo(false, 1, 2, 4, 5, 7);
                        cboWMFrequency.SelectedIndex = 1;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "191 kbps":
                        FormatWMFrequencyCombo(false, 0, 1, 3, 4);
                        FormatWMChannelsCombo(false, 0, 1, 3, 4, 6, 7);
                        cboWMFrequency.SelectedIndex = 2;
                        cboWMChannels.SelectedIndex = 2;
                        break;
                    case "192 kbps":
                        FormatWMFrequencyCombo(false, 0);
                        FormatWMChannelsCombo(false, 2, 5, 6, 7);
                        cboWMFrequency.SelectedIndex = 1;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "255 kbps":
                    case "383 kbps":
                        FormatWMFrequencyCombo(false, 0, 1, 3, 4);
                        FormatWMChannelsCombo(false, 0, 1, 2, 3, 4, 6, 7);
                        cboWMFrequency.SelectedIndex = 2;
                        cboWMChannels.SelectedIndex = 5;
                        break;
                    case "256 kbps":
                        FormatWMFrequencyCombo(false, 0);
                        FormatWMChannelsCombo(false, 2, 5, 6, 7);
                        cboWMFrequency.SelectedIndex = 1;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "384 kbps":
                        FormatWMFrequencyCombo(false, 0);
                        FormatWMChannelsCombo(false, 0, 2, 5, 6);
                        cboWMFrequency.SelectedIndex = 1;
                        cboWMChannels.SelectedIndex = 1;
                        break;
                    case "440 kbps":
                        FormatWMFrequencyCombo(false, 0);
                        FormatWMChannelsCombo(false, 0, 2, 5, 6, 7);
                        cboWMFrequency.SelectedIndex = 1;
                        cboWMChannels.SelectedIndex = 1;
                        break;
                    case "640 kbps":
                        FormatWMFrequencyCombo(false, 0, 3);
                        FormatWMChannelsCombo(false, 0, 1, 2, 5, 6, 7);
                        cboWMFrequency.SelectedIndex = 1;
                        cboWMChannels.SelectedIndex = 3;
                        break;
                    case "768 kbps":
                        FormatWMFrequencyCombo(false, 0, 3);
                        FormatWMChannelsCombo(false, 0, 1, 2, 3, 5, 6);
                        cboWMFrequency.SelectedIndex = 1;
                        cboWMChannels.SelectedIndex = 4;
                        break;
                }
            }
        }

        private void FrequencyAudio10Changed()
        {
            FormatWMFrequencyCombo(true);

            if (rbtWMQualityVbr.Checked)
            {
                if (chkWM2PassEncoding.Checked)
                {
                    switch (cboWMFrequency.Text)
                    {
                        case "44 kHz":
                            FormatWmBitrateCombo(true);
                            FormatWMChannelsCombo(true);
                            break;
                        case "48 kHz":
                            FormatWmBitrateCombo(false, 0, 1);
                            FormatWMChannelsCombo(false, 0, 2);
                            cboWMBitrate.SelectedIndex = 2;
                            cboWMChannels.SelectedIndex = 1;
                            break;
                        case "88 kHz":
                            FormatWmBitrateCombo(false, 0, 1, 7, 8);
                            FormatWMChannelsCombo(false, 0, 2, 3);
                            cboWMBitrate.SelectedIndex = 2;
                            cboWMChannels.SelectedIndex = 1;
                            break;
                        case "96 kHz":
                            FormatWmBitrateCombo(false, 0, 1);
                            FormatWMChannelsCombo(false, 1, 3);
                            cboWMBitrate.SelectedIndex = 2;
                            cboWMChannels.SelectedIndex = 0;
                            break;
                    }
                }
                else
                {
                    switch (cboWMFrequency.Text)
                    {
                        case "44 kHz":
                            FormatWmBitrateCombo(true);
                            FormatWMChannelsCombo(false, 2);
                            cboWMChannels.SelectedIndex = 0;
                            break;
                        case "48 kHz":
                            FormatWmBitrateCombo(true);
                            FormatWMChannelsCombo(false, 1);
                            cboWMChannels.SelectedIndex = 0;
                            break;
                        case "88 kHz":
                        case "96 kHz":
                            FormatWmBitrateCombo(false, 0, 1, 2);
                            FormatWMChannelsCombo(false, 1);
                            cboWMBitrate.SelectedIndex = 3;
                            cboWMChannels.SelectedIndex = 0;
                            break;
                    }
                }
            }

            if (rbtWMBitrateCbr.Checked)
            {
                switch (cboWMFrequency.Text)
                {
                    case "32 kHz":
                        FormatWmBitrateCombo(false, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
                        FormatWMChannelsCombo(false, 1, 2, 3, 4, 5, 6, 7);
                        cboWMBitrate.SelectedIndex = 0;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "44 kHz":
                        FormatWmBitrateCombo(false, 0, 5, 8, 10, 12);
                        FormatWMChannelsCombo(false, 1, 2, 3, 4, 5, 6, 7);
                        cboWMBitrate.SelectedIndex = 1;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "48 kHz":
                        FormatWmBitrateCombo(false, 0);
                        FormatWMChannelsCombo(true);
                        cboWMBitrate.SelectedIndex = 1;
                        break;
                    case "88 kHz":
                        FormatWmBitrateCombo(false, 0, 1, 2, 3, 4, 5, 7, 8, 10, 12, 15, 16);
                        FormatWMChannelsCombo(false, 2, 5, 7);
                        cboWMBitrate.SelectedIndex = 6;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                    case "96 kHz":
                        FormatWmBitrateCombo(false, 0, 1, 2, 3, 4, 5, 7, 8, 10, 12);
                        FormatWMChannelsCombo(false, 2, 5, 7);
                        cboWMBitrate.SelectedIndex = 6;
                        cboWMChannels.SelectedIndex = 0;
                        break;
                }
            }
        }

        private void ChannelsAudio10Changed()
        {
            FormatWMChannelsCombo(true);

            if (rbtWMQualityVbr.Checked)
            {
                if (chkWM2PassEncoding.Checked)
                {
                    switch (cboWMChannels.Text)
                    {
                        case "2 channel 16 bit VBR":
                            FormatWmBitrateCombo(false, 2, 3, 4, 5, 6, 7, 8);
                            FormatWMFrequencyCombo(false, 1, 2, 3);
                            cboWMBitrate.SelectedIndex = 0;
                            cboWMFrequency.SelectedIndex = 0;
                            break;
                        case "2 channel 24 bit VBR":
                            FormatWmBitrateCombo(false, 0, 1, 7, 8);
                            FormatWMFrequencyCombo(true);
                            cboWMBitrate.SelectedIndex = 2;
                            break;
                        case "5.1 channel 16 bit VBR":
                            FormatWmBitrateCombo(false, 0, 1, 7, 8);
                            FormatWMFrequencyCombo(false, 1, 2, 3);
                            cboWMBitrate.SelectedIndex = 2;
                            cboWMFrequency.SelectedIndex = 0;
                            break;
                        case "5.1 channel 24 bit VBR":
                            FormatWmBitrateCombo(false, 0, 1);
                            FormatWMFrequencyCombo(false, 2);
                            cboWMBitrate.SelectedIndex = 2;
                            cboWMFrequency.SelectedIndex = 0;
                            break;
                    }
                }
                else
                {
                    switch (cboWMChannels.Text)
                    {
                        case "2 channel 24 bit VBR":
                            FormatWmBitrateCombo(true);
                            FormatWMFrequencyCombo(true);
                            break;
                        case "5.1 channel 16 bit VBR":
                            FormatWmBitrateCombo(true);
                            FormatWMFrequencyCombo(false, 1, 2, 3);
                            cboWMFrequency.SelectedIndex = 0;
                            break;
                        case "5.1 channel 24 bit VBR":
                            FormatWmBitrateCombo(true);
                            FormatWMFrequencyCombo(false, 0);
                            cboWMFrequency.SelectedIndex = 1;
                            break;
                    }
                }
            }

            if (rbtWMBitrateCbr.Checked)
            {
                switch (cboWMChannels.Text)
                {
                    case "2 channel 16 bit CBR":
                        FormatWmBitrateCombo(false, 5, 8, 10, 12, 13, 14, 15, 16);
                        FormatWMFrequencyCombo(false, 1, 2, 3, 4);
                        cboWMBitrate.SelectedIndex = 0;
                        cboWMFrequency.SelectedIndex = 0;
                        break;
                    case "2 channel 24 bit CBR":
                        FormatWmBitrateCombo(false, 0, 1, 2, 3, 4, 5, 7, 8, 10, 12, 15, 16);
                        FormatWMFrequencyCombo(false, 0);
                        cboWMBitrate.SelectedIndex = 6;
                        cboWMFrequency.SelectedIndex = 1;
                        break;
                    case "2 channel 24 bit Low Delay CBR":
                        FormatWmBitrateCombo(false, 0, 1, 2, 3, 4, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16);
                        FormatWMFrequencyCombo(false, 0, 1, 3, 4);
                        cboWMBitrate.SelectedIndex = 5;
                        cboWMFrequency.SelectedIndex = 2;
                        break;
                    case "5.1 channel 16 bit CBR":
                        FormatWmBitrateCombo(false, 0, 1, 2, 3, 4, 5, 8, 10, 12, 15, 16);
                        FormatWMFrequencyCombo(false, 0, 3, 4);
                        cboWMBitrate.SelectedIndex = 6;
                        cboWMFrequency.SelectedIndex = 1;
                        break;
                    case "5.1 channel 24 bit CBR":
                        FormatWmBitrateCombo(false, 0, 1, 2, 3, 4, 5, 7, 8, 10, 12);
                        FormatWMFrequencyCombo(false, 0, 3);
                        cboWMBitrate.SelectedIndex = 6;
                        cboWMFrequency.SelectedIndex = 1;
                        break;
                    case "5.1 channel 24 bit Low Delay CBR":
                        FormatWmBitrateCombo(false, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 14, 15, 16);
                        FormatWMFrequencyCombo(false, 0, 1, 3, 4);
                        cboWMBitrate.SelectedIndex = 8;
                        cboWMFrequency.SelectedIndex = 2;
                        break;
                    case "7.1 channel 16 bit CBR":
                        FormatWmBitrateCombo(false, 0, 1, 2, 3, 4, 5, 8, 9, 11, 12, 13, 14, 15, 16);
                        FormatWMFrequencyCombo(false, 0, 1, 3, 4);
                        cboWMBitrate.SelectedIndex = 6;
                        cboWMFrequency.SelectedIndex = 2;
                        break;
                    case "7.1 channel 24 bit CBR":
                        FormatWmBitrateCombo(false, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15);
                        FormatWMFrequencyCombo(false, 0, 1, 3);
                        cboWMBitrate.SelectedIndex = 13;
                        cboWMFrequency.SelectedIndex = 2;
                        break;
                }
            }
        }

        #endregion

        #endregion

        #region Panel Test Conversion Event.

        #endregion

        #region Panel Nero AAC Events.

        private void rbtNeroAacQualityVbr_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtNeroAacQualityVbr.Checked)
            {
                trbNeroAacBitRate.Minimum = 0;
                trbNeroAacBitRate.Maximum = 11;
                trbNeroAacBitRate.TickFrequency = 1;
                trbNeroAacBitRate.Value = AudioConverterSettings.Default.trbNeroAacBitRateVbr_Value;
                _neroAacSelectionFirstValue = 0;
                _neroAacQuality = "q";
            }
        }

        private void rbtNeroAacBitrateAbr_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtNeroAacBitrateAbr.Checked)
            {
                trbNeroAacBitRate.Minimum = 0;
                trbNeroAacBitRate.Maximum = 15;
                trbNeroAacBitRate.TickFrequency = 1;
                trbNeroAacBitRate.Value = AudioConverterSettings.Default.trbNeroAacBitRateAbr_Value;
                _neroAacSelectionFirstValue = 1;
                _neroAacQuality = "br";
            }
        }

        private void rbtNeroAacBitrateCbr_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtNeroAacBitrateCbr.Checked)
            {
                trbNeroAacBitRate.Minimum = 0;
                trbNeroAacBitRate.Maximum = 15;
                trbNeroAacBitRate.TickFrequency = 1;
                trbNeroAacBitRate.Value = AudioConverterSettings.Default.trbNeroAacBitRateCbr_Value;
                _neroAacSelectionFirstValue = 2;
                _neroAacQuality = "cbr";
            }
        }

        private void trbNeroAacBitRate_ValueChanged(object sender, EventArgs e)
        {
            if (rbtNeroAacQualityVbr.Checked)
            {
                ConvertNeroAacVbrValues();
                AudioConverterSettings.Default.trbNeroAacBitRateVbr_Value = trbNeroAacBitRate.Value;
            }

            if (rbtNeroAacBitrateAbr.Checked)
            {
                ConvertNeroAacAbrCbrValues();
                AudioConverterSettings.Default.trbNeroAacBitRateAbr_Value = trbNeroAacBitRate.Value;
            }

            if (rbtNeroAacBitrateCbr.Checked)
            {
                ConvertNeroAacAbrCbrValues();
                AudioConverterSettings.Default.trbNeroAacBitRateCbr_Value = trbNeroAacBitRate.Value;
            }

            _neroAacSelectionSecondValue = trbNeroAacBitRate.Value;
        }

        private void cboNeroAacForcedProfile_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboNeroAacForcedProfile.SelectedIndex)
            {
                case 0:
                    _neroAacProfile = string.Empty;
                    break;
                case 1:
                    _neroAacProfile = string.Format("-lc");
                    break;
                case 2:
                    _neroAacProfile = string.Format("-he");
                    break;
                case 3:
                    _neroAacProfile = string.Format("-hev2");
                    break;
            }

            _neroAacSelectionThirdValue = cboNeroAacForcedProfile.SelectedIndex;
        }

        private void btnNeroAacEncoder_MouseClick(object sender, MouseEventArgs e)
        {
            if (!string.IsNullOrEmpty(_neroAacEncoderLocation))
            {
                mnuNeroAacCurrentEncoder.Text = string.Format("Current Encoder {0}", _neroAacEncoderLocation);
            }
            else
            {
                mnuNeroAacCurrentEncoder.Text = string.Format("Current Encoder");
            }

            ctmNeroAac.Show(btnNeroAacEncoder, e.X, e.Y);
        }

        private void mnuNeroAacLocateEncoder_Click(object sender, EventArgs e)
        {
            openFileDialog.Title = @"Choose Nero (AAC) Encoder";
            openFileDialog.Filter = @"CLI Encoder|neroAacEnc.exe";
            openFileDialog.FilterIndex = 1;
            string path = Path.GetDirectoryName(_neroAacEncoderLocation);

            if (!string.IsNullOrEmpty(path) && Directory.Exists(path))
            {
                openFileDialog.InitialDirectory = Path.GetDirectoryName(_neroAacEncoderLocation);
            }
            else
            {
                openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
            }

            DialogResult result = openFileDialog.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                _neroAacEncoderLocation = openFileDialog.FileName;
                mnuNeroAacCurrentEncoder.Text = string.Format("Current Encoder {0}", _neroAacEncoderLocation);
            }
        }

        #endregion

        #region MP3 Settings.

        private void ConvertMp3VbrValues()
        {
            switch (trbMp3Bitrate.Value)
            {
                case 1:
                    _mp3Bitrate = "-V 9";
                    _mp3BitrateValue = 70;
                    _mp3BitrateDescription = string.Empty;
                    break;
                case 2:
                    _mp3Bitrate = "-V 8.5";
                    _mp3BitrateValue = 75;
                    _mp3BitrateDescription = string.Empty;
                    break;
                case 3:
                    _mp3Bitrate = "-V 8";
                    _mp3BitrateValue = 80;
                    _mp3BitrateDescription = string.Empty;
                    break;
                case 4:
                    _mp3Bitrate = "-V 7.5";
                    _mp3BitrateValue = 90;
                    _mp3BitrateDescription = string.Empty;
                    break;
                case 5:
                    _mp3Bitrate = "-V 7";
                    _mp3BitrateValue = 100;
                    _mp3BitrateDescription = string.Empty;
                    break;
                case 6:
                    _mp3Bitrate = "-V 6.5";
                    _mp3BitrateValue = 110;
                    _mp3BitrateDescription = string.Empty;
                    break;
                case 7:
                    _mp3Bitrate = "-V 6";
                    _mp3BitrateValue = 120;
                    _mp3BitrateDescription = string.Empty;
                    break;
                case 8:
                    _mp3Bitrate = "-V 5.5";
                    _mp3BitrateValue = 125;
                    _mp3BitrateDescription = string.Empty;
                    break;
                case 9:
                    _mp3Bitrate = "-V 5";
                    _mp3BitrateValue = 130;
                    _mp3BitrateDescription = string.Empty;
                    break;
                case 10:
                    _mp3Bitrate = "-V 4.5";
                    _mp3BitrateValue = 145;
                    _mp3BitrateDescription = string.Empty;
                    break;
                case 11:
                    _mp3Bitrate = "-V 4";
                    _mp3BitrateValue = 160;
                    _mp3BitrateDescription = "\"medium\"";
                    break;
                case 12:
                    _mp3Bitrate = "-V 3.5";
                    _mp3BitrateValue = 165;
                    _mp3BitrateDescription = string.Empty;
                    break;
                case 13:
                    _mp3Bitrate = "-V 3";
                    _mp3BitrateValue = 170;
                    _mp3BitrateDescription = string.Empty;
                    break;
                case 14:
                    _mp3Bitrate = "-V 2.5";
                    _mp3BitrateValue = 180;
                    _mp3BitrateDescription = string.Empty;
                    break;
                case 15:
                    _mp3Bitrate = "-V 2";
                    _mp3BitrateValue = 190;
                    _mp3BitrateDescription = "\"standard\"";
                    break;
                case 16:
                    _mp3Bitrate = "-V 1.5";
                    _mp3BitrateValue = 205;
                    _mp3BitrateDescription = string.Empty;
                    break;
                case 17:
                    _mp3Bitrate = "-V 1";
                    _mp3BitrateValue = 220;
                    _mp3BitrateDescription = string.Empty;
                    break;
                case 18:
                    _mp3Bitrate = "-V 0.5";
                    _mp3BitrateValue = 230;
                    _mp3BitrateDescription = string.Empty;
                    break;
                case 19:
                    _mp3Bitrate = "-V 0";
                    _mp3BitrateValue = 240;
                    _mp3BitrateDescription = "\"extreme\"";
                    break;
            }

            lblMp3Bitrate.Text = string.Format("Variable Bit Rate Quality: {0}", _mp3Bitrate);
            lblMp3Info.Text = string.Format("[estimated bit rate {0} kbps]     {1}", _mp3BitrateValue, _mp3BitrateDescription);
        }

        private void ConvertMp3FreeFormatValues()
        {
            _mp3BitrateValue = trbMp3Bitrate.Value;
            _mp3Bitrate = string.Format("-b {0} --freeformat", _mp3BitrateValue);

            lblMp3Bitrate.Text = string.Format("Free Format: {0}", _mp3BitrateValue);
            lblMp3Info.Text = string.Format("[check player compability]");
        }

        private void ConvertMp3AbrCbrValues()
        {
            if (trbMp3Bitrate.Value <= 8)
            {
                _mp3BitrateValue = trbMp3Bitrate.Value * 8;

                if (trbMp3Bitrate.Value < 4)
                {
                    lblMp3Info.Text = string.Format("[sample rates: 8, 11, 12, 16, 22 & 24 KHz]");
                }
                else
                {
                    lblMp3Info.Text = string.Empty;
                }
            }

            if (trbMp3Bitrate.Value > 8 && trbMp3Bitrate.Value <= 14)
            {
                _mp3BitrateValue = trbMp3Bitrate.Value * 8 + (8 * (trbMp3Bitrate.Value - 8));

                if (trbMp3Bitrate.Value == 13)
                {
                    lblMp3Info.Text = string.Format("[sample rates: 8, 11, 12, 16, 22 & 24 KHz]");
                }
                else
                {
                    lblMp3Info.Text = string.Empty;
                }
            }

            if (trbMp3Bitrate.Value == 15)
            {
                _mp3BitrateValue = 192;
                lblMp3Info.Text = string.Format("[sample rates: 32, 44.1 & 48 KHz]");
            }

            if (trbMp3Bitrate.Value == 16)
            {
                _mp3BitrateValue = 224;
                lblMp3Info.Text = string.Format("[sample rates: 32, 44.1 & 48 KHz]");
            }

            if (trbMp3Bitrate.Value == 17)
            {
                _mp3BitrateValue = 256;
                lblMp3Info.Text = string.Format("[sample rates: 32, 44.1 & 48 KHz]");
            }

            if (trbMp3Bitrate.Value == 18)
            {
                _mp3BitrateValue = 320;
                lblMp3Info.Text = string.Format("[sample rates: 32, 44.1 & 48 KHz]");
            }

            if (rbtMp3BitrateCbr.Checked)
            {
                lblMp3Bitrate.Text = string.Format("Constant Bit Rate: {0} kbps", _mp3BitrateValue);
            }
            else
            {
                lblMp3Bitrate.Text = string.Format("Average Bit Rate: {0} kbps", _mp3BitrateValue);
            }

            if (rbtMp3BitrateAbr.Checked)
            {
                _mp3Bitrate = string.Format("-b {0} --abr", _mp3BitrateValue);
            }
            else
            {
                _mp3Bitrate = string.Format("-b {0}", _mp3BitrateValue);
            }
        }
        
        #endregion

        #region SHN Settings.

        #endregion

        #region AIFF Settings.

        private void LoadAiffBitDephtCombo()
        {
            cboAiffBitDepht.Add(imlSettings.Images["Bits"], "[as source]");
            cboAiffBitDepht.Add(imlSettings.Images["BitRate"], "8 bit");
            cboAiffBitDepht.Add(imlSettings.Images["BitRate"], "16 bit (CD)");
            cboAiffBitDepht.Add(imlSettings.Images["BitRate"], "24 bit (DVD)");
            cboAiffBitDepht.Add(imlSettings.Images["BitRate"], "32 bit");
        }

        private void LoadAiffSampleRateCombo()
        {
            cboAiffSampleRate.Add(imlSettings.Images["Frequency"], "[as source]");
            cboAiffSampleRate.Add(imlSettings.Images["Frequency"], "8 KHz");
            cboAiffSampleRate.Add(imlSettings.Images["Frequency"], "11 KHz");
            cboAiffSampleRate.Add(imlSettings.Images["Frequency"], "12 KHz");
            cboAiffSampleRate.Add(imlSettings.Images["Frequency"], "16 KHz");
            cboAiffSampleRate.Add(imlSettings.Images["Frequency"], "22 KHz");
            cboAiffSampleRate.Add(imlSettings.Images["Frequency"], "24 KHz");
            cboAiffSampleRate.Add(imlSettings.Images["Frequency"], "32 KHz");
            cboAiffSampleRate.Add(imlSettings.Images["Frequency"], "44,1 KHz (CD)");
            cboAiffSampleRate.Add(imlSettings.Images["Frequency"], "48 KHz (DAT)");
            cboAiffSampleRate.Add(imlSettings.Images["Frequency"], "96 KHz (DVD)");
            cboAiffSampleRate.Add(imlSettings.Images["Frequency"], "192 KHz");
        }

        private void LoadAiffChannelsCombo()
        {
            cboAiffChannels.Add(imlSettings.Images["Ch7"], "[as source]");
            cboAiffChannels.Add(imlSettings.Images["Ch1"], "1 'mono'");
            cboAiffChannels.Add(imlSettings.Images["Ch2"], "2 'stereo'");
            cboAiffChannels.Add(imlSettings.Images["Ch3"], "3");
            cboAiffChannels.Add(imlSettings.Images["Ch4"], "4 'quadraphonic'");
            cboAiffChannels.Add(imlSettings.Images["Ch5"], "5 'surround'");
            cboAiffChannels.Add(imlSettings.Images["Ch5"], "6 '5.1 surround' (DVD)");
            cboAiffChannels.Add(imlSettings.Images["Ch7"], "7");
            cboAiffChannels.Add(imlSettings.Images["Ch7"], "8 '7.1 theater'");
        }

        #endregion

        #region Ogg Vorbis Settings

        private void ConvertOggVorbisAbrCbrValues()
        {
            switch (trbOggVorbisBitRate.Value)
            {
                case 0:
                    _oggVorbisBitRateValue = 48;
                    _oggVorbisBitRateDescription = "48 kbps";
                    break;
                case 1:
                    _oggVorbisBitRateValue = 64;
                    _oggVorbisBitRateDescription = "64 kbps";
                    break;
                case 2:
                    _oggVorbisBitRateValue = 80;
                    _oggVorbisBitRateDescription = "80 kbps";
                    break;
                case 3:
                    _oggVorbisBitRateValue = 96;
                    _oggVorbisBitRateDescription = "96 kbps";
                    break;
                case 4:
                    _oggVorbisBitRateValue = 112;
                    _oggVorbisBitRateDescription = "112 kbps";
                    break;
                case 5:
                    _oggVorbisBitRateValue = 128;
                    _oggVorbisBitRateDescription = "128 kbps";
                    break;
                case 6:
                    _oggVorbisBitRateValue = 160;
                    _oggVorbisBitRateDescription = "160 kbps";
                    break;
                case 7:
                    _oggVorbisBitRateValue = 192;
                    _oggVorbisBitRateDescription = "192 kbps";
                    break;
                case 8:
                    _oggVorbisBitRateValue = 224;
                    _oggVorbisBitRateDescription = "224 kbps";
                    break;
                case 9:
                    _oggVorbisBitRateValue = 256;
                    _oggVorbisBitRateDescription = "256 kbps";
                    break;
                case 10:
                    _oggVorbisBitRateValue = 350;
                    _oggVorbisBitRateDescription = "350 kbps";
                    break;
                case 11:
                    _oggVorbisBitRateValue = 500;
                    _oggVorbisBitRateDescription = "500 kbps";
                    break;
            }

            _oggVorbisBitRate = string.Format("-b=\"{0}\"", _oggVorbisBitRateValue);

            if (rbtOggVorbisAbr.Checked)
            {
                lblOggVorbisBitRate.Text = string.Format("Average Bit Rate: {0} kbps", _oggVorbisBitRateValue);
            }
            
            if(rbtOggVorbisCbr.Checked)
            {
                lblOggVorbisBitRate.Text = string.Format("Constant Bit Rate: {0} kbps", _oggVorbisBitRateValue);
                _oggVorbisBitRate = string.Format("--managed {0}", _oggVorbisBitRate);
            }
        }

        private void ConvertOggVorbisVbrValues()
        {
            switch (trbOggVorbisBitRate.Value)
            {
                case 0:
                    _oggVorbisBitRateValue = -0.1;
                    _oggVorbisBitRateDescription = "48kbps";
                    break;
                case 1:
                    _oggVorbisBitRateValue = 0;
                    _oggVorbisBitRateDescription = "64kbps";
                    break;
                case 2:
                    _oggVorbisBitRateValue = 0.1;
                    _oggVorbisBitRateDescription = "80kbps";
                    break;
                case 3:
                    _oggVorbisBitRateValue = 0.2;
                    _oggVorbisBitRateDescription = "96kbps";
                    break;
                case 4:
                    _oggVorbisBitRateValue = 0.3;
                    _oggVorbisBitRateDescription = "112kbps";
                    break;
                case 5:
                    _oggVorbisBitRateValue = 0.4;
                    _oggVorbisBitRateDescription = "128kbps";
                    break;
                case 6:
                    _oggVorbisBitRateValue = 0.5;
                    _oggVorbisBitRateDescription = "160kbps";
                    break;
                case 7:
                    _oggVorbisBitRateValue = 0.6;
                    _oggVorbisBitRateDescription = "192kbps";
                    break;
                case 8:
                    _oggVorbisBitRateValue = 0.7;
                    _oggVorbisBitRateDescription = "224kbps";
                    break;
                case 9:
                    _oggVorbisBitRateValue = 0.8;
                    _oggVorbisBitRateDescription = "256kbps";
                    break;
                case 10:
                    _oggVorbisBitRateValue = 0.9;
                    _oggVorbisBitRateDescription = "350kbps";
                    break;
                case 11:
                    _oggVorbisBitRateValue = 1;
                    _oggVorbisBitRateDescription = "500kbps";
                    break;
            }

            _oggVorbisBitRate = string.Format("-q=\"{0}\"", _oggVorbisBitRateValue);

            lblOggVorbisBitRate.Text = string.Format("Variable Bit Rate Quality: -q {0}", _oggVorbisBitRateValue);
            lblOggVorbisInfo.Text = string.Format("[estimated bit rate {0}]", _oggVorbisBitRateDescription);
        }

        private void ConvertOggVorbisVbrFinerValues()
        {
            switch (trbOggVorbisBitRate.Value)
            {
                case 0:
                    _oggVorbisBitRateValue = -0.1;
                    _oggVorbisBitRateDescription = "48kbps";
                    break;
                case 1:
                    _oggVorbisBitRateValue = -0.075;
                    _oggVorbisBitRateDescription = "52kbps";
                    break;
                case 2:
                    _oggVorbisBitRateValue = -0.05;
                    _oggVorbisBitRateDescription = "56kbps";
                    break;
                case 3:
                    _oggVorbisBitRateValue = -0.025;
                    _oggVorbisBitRateDescription = "96kbps";
                    break;
                case 4:
                    _oggVorbisBitRateValue = 0;
                    _oggVorbisBitRateDescription = "64kbps";
                    break;
                case 5:
                    _oggVorbisBitRateValue = 0.025;
                    _oggVorbisBitRateDescription = "68kbps";
                    break;
                case 6:
                    _oggVorbisBitRateValue = 0.05;
                    _oggVorbisBitRateDescription = "72kbps";
                    break;
                case 7:
                    _oggVorbisBitRateValue = 0.075;
                    _oggVorbisBitRateDescription = "76kbps";
                    break;
                case 8:
                    _oggVorbisBitRateValue = 0.1;
                    _oggVorbisBitRateDescription = "80kbps";
                    break;
                case 9:
                    _oggVorbisBitRateValue = 0.125;
                    _oggVorbisBitRateDescription = "84kbps";
                    break;
                case 10:
                    _oggVorbisBitRateValue = 0.15;
                    _oggVorbisBitRateDescription = "88kbps";
                    break;
                case 11:
                    _oggVorbisBitRateValue = 0.175;
                    _oggVorbisBitRateDescription = "92kbps";
                    break;
                case 12:
                    _oggVorbisBitRateValue = 0.2;
                    _oggVorbisBitRateDescription = "96kbps";
                    break;
                case 13:
                    _oggVorbisBitRateValue = 0.225;
                    _oggVorbisBitRateDescription = "100kbps";
                    break;
                case 14:
                    _oggVorbisBitRateValue = 0.25;
                    _oggVorbisBitRateDescription = "104kbps";
                    break;
                case 15:
                    _oggVorbisBitRateValue = 0.275;
                    _oggVorbisBitRateDescription = "108kbps";
                    break;
                case 16:
                    _oggVorbisBitRateValue = 0.3;
                    _oggVorbisBitRateDescription = "112kbps";
                    break;
                case 17:
                    _oggVorbisBitRateValue = 0.325;
                    _oggVorbisBitRateDescription = "116kbps";
                    break;
                case 18:
                    _oggVorbisBitRateValue = 0.35;
                    _oggVorbisBitRateDescription = "120kbps";
                    break;
                case 19:
                    _oggVorbisBitRateValue = 0.375;
                    _oggVorbisBitRateDescription = "124kbps";
                    break;
                case 20:
                    _oggVorbisBitRateValue = 0.4;
                    _oggVorbisBitRateDescription = "128kbps";
                    break;
                case 21:
                    _oggVorbisBitRateValue = 0.425;
                    _oggVorbisBitRateDescription = "136kbps";
                    break;
                case 22:
                    _oggVorbisBitRateValue = 0.45;
                    _oggVorbisBitRateDescription = "144kbps";
                    break;
                case 23:
                    _oggVorbisBitRateValue = 0.475;
                    _oggVorbisBitRateDescription = "152kbps";
                    break;
                case 24:
                    _oggVorbisBitRateValue = 0.5;
                    _oggVorbisBitRateDescription = "160kbps";
                    break;
                case 25:
                    _oggVorbisBitRateValue = 0.525;
                    _oggVorbisBitRateDescription = "168kbps";
                    break;
                case 26:
                    _oggVorbisBitRateValue = 0.55;
                    _oggVorbisBitRateDescription = "176kbps";
                    break;
                case 27:
                    _oggVorbisBitRateValue = 0.575;
                    _oggVorbisBitRateDescription = "184kbps";
                    break;
                case 28:
                    _oggVorbisBitRateValue = 0.6;
                    _oggVorbisBitRateDescription = "192kbps";
                    break;
                case 29:
                    _oggVorbisBitRateValue = 0.625;
                    _oggVorbisBitRateDescription = "200kbps";
                    break;
                case 30:
                    _oggVorbisBitRateValue = 0.65;
                    _oggVorbisBitRateDescription = "208kbps";
                    break;
                case 31:
                    _oggVorbisBitRateValue = 0.675;
                    _oggVorbisBitRateDescription = "216kbps";
                    break;
                case 32:
                    _oggVorbisBitRateValue = 0.7;
                    _oggVorbisBitRateDescription = "224kbps";
                    break;
                case 33:
                    _oggVorbisBitRateValue = 0.725;
                    _oggVorbisBitRateDescription = "232kbps";
                    break;
                case 34:
                    _oggVorbisBitRateValue = 0.75;
                    _oggVorbisBitRateDescription = "240kbps";
                    break;
                case 35:
                    _oggVorbisBitRateValue = 0.775;
                    _oggVorbisBitRateDescription = "248kbps";
                    break;
                case 36:
                    _oggVorbisBitRateValue = 0.8;
                    _oggVorbisBitRateDescription = "256kbps";
                    break;
                case 37:
                    _oggVorbisBitRateValue = 0.825;
                    _oggVorbisBitRateDescription = "278kbps";
                    break;
                case 38:
                    _oggVorbisBitRateValue = 0.85;
                    _oggVorbisBitRateDescription = "302kbps";
                    break;
                case 39:
                    _oggVorbisBitRateValue = 0.875;
                    _oggVorbisBitRateDescription = "326kbps";
                    break;
                case 40:
                    _oggVorbisBitRateValue = 0.9;
                    _oggVorbisBitRateDescription = "350kbps";
                    break;
                case 41:
                    _oggVorbisBitRateValue = 0.925;
                    _oggVorbisBitRateDescription = "388kbps";
                    break;
                case 42:
                    _oggVorbisBitRateValue = 0.95;
                    _oggVorbisBitRateDescription = "425kbps";
                    break;
                case 43:
                    _oggVorbisBitRateValue = 0.975;
                    _oggVorbisBitRateDescription = "462kbps";
                    break;
                case 44:
                    _oggVorbisBitRateValue = 1;
                    _oggVorbisBitRateDescription = "500kbps";
                    break;
            }

            _oggVorbisBitRate = string.Format("-q=\"{0}\"", _oggVorbisBitRateValue);

            lblOggVorbisBitRate.Text = string.Format("Variable Bit Rate Quality: -q {0}", _oggVorbisBitRateValue);
            lblOggVorbisInfo.Text = string.Format("[estimated bit rate {0}]", _oggVorbisBitRateDescription);
        }

        private void LoadOggVorbisFrequency()
        {
            cboOggVorbisFrequency.Add(imlSettings.Images["Frequency"], "[as source]");
            cboOggVorbisFrequency.Add(imlSettings.Images["Frequency"], "8 KHz");
            cboOggVorbisFrequency.Add(imlSettings.Images["Frequency"], "11 KHz");
            cboOggVorbisFrequency.Add(imlSettings.Images["Frequency"], "12 KHz");
            cboOggVorbisFrequency.Add(imlSettings.Images["Frequency"], "16 KHz");
            cboOggVorbisFrequency.Add(imlSettings.Images["Frequency"], "22 KHz");
            cboOggVorbisFrequency.Add(imlSettings.Images["Frequency"], "24 KHz");
            cboOggVorbisFrequency.Add(imlSettings.Images["Frequency"], "32 KHz");
            cboOggVorbisFrequency.Add(imlSettings.Images["Frequency"], "44,1 KHz (CD)");
            cboOggVorbisFrequency.Add(imlSettings.Images["Frequency"], "48 KHz (DAT)");
        }

        private void LoadOggVorbisChannelsCombo()
        {
            cboOggVorbisChannels.Add(imlSettings.Images["Ch2"], "[as source]");
            cboOggVorbisChannels.Add(imlSettings.Images["Ch1"], "1 'mono'");
            cboOggVorbisChannels.Add(imlSettings.Images["Ch2"], "2 'stereo' (CD)");
        }

        #endregion

        #region Wave Settings

        private void LoadWaveBitDephtCombo()
        {
            cboWaveBitDepht.Add(imlSettings.Images["Bits"], "[as source]");
            cboWaveBitDepht.Add(imlSettings.Images["BitRate"], "8 bit");
            cboWaveBitDepht.Add(imlSettings.Images["BitRate"], "16 bit (CD)");
            cboWaveBitDepht.Add(imlSettings.Images["BitRate"], "24 bit (DVD)");
            cboWaveBitDepht.Add(imlSettings.Images["BitRate"], "32 bit");
            cboWaveBitDepht.Add(imlSettings.Images["BitRate"], "32 bit IEEE float");
        }

        private void LoadWaveSampleRateCombo()
        {
            cboWaveSampleRate.Add(imlSettings.Images["Frequency"], "[as source]");
            cboWaveSampleRate.Add(imlSettings.Images["Frequency"], "8 KHz");
            cboWaveSampleRate.Add(imlSettings.Images["Frequency"], "11 KHz");
            cboWaveSampleRate.Add(imlSettings.Images["Frequency"], "12 KHz");
            cboWaveSampleRate.Add(imlSettings.Images["Frequency"], "16 KHz");
            cboWaveSampleRate.Add(imlSettings.Images["Frequency"], "22 KHz");
            cboWaveSampleRate.Add(imlSettings.Images["Frequency"], "24 KHz");
            cboWaveSampleRate.Add(imlSettings.Images["Frequency"], "32 KHz");
            cboWaveSampleRate.Add(imlSettings.Images["Frequency"], "44,1 KHz (CD)");
            cboWaveSampleRate.Add(imlSettings.Images["Frequency"], "48 KHz (DAT)");
            cboWaveSampleRate.Add(imlSettings.Images["Frequency"], "96 KHz (DVD)");
            cboWaveSampleRate.Add(imlSettings.Images["Frequency"], "192 KHz");
        }

        private void LoadWaveChannelsCombo()
        {
            cboWaveChannels.Add(imlSettings.Images["Ch7"], "[as source]");
            cboWaveChannels.Add(imlSettings.Images["Ch1"], "1 'mono'");
            cboWaveChannels.Add(imlSettings.Images["Ch2"], "2 'stereo'");
            cboWaveChannels.Add(imlSettings.Images["Ch3"], "3");
            cboWaveChannels.Add(imlSettings.Images["Ch4"], "4 'quadraphonic'");
            cboWaveChannels.Add(imlSettings.Images["Ch5"], "5 'surround'");
            cboWaveChannels.Add(imlSettings.Images["Ch5"], "6 '5.1 surround' (DVD)");
            cboWaveChannels.Add(imlSettings.Images["Ch7"], "7");
            cboWaveChannels.Add(imlSettings.Images["Ch7"], "8 '7.1 theater'");
        }

        #endregion

        #region HelperMethods.

        private void FormatWmBitrateCombo(bool reset, params int[] index)
        {
            foreach (ComboBoxItem item in cboWMBitrate.Items)
            {
                item.ForeColor = Color.Black;
            }

            if (!reset)
            {
                for (int i = 0; i < index.Length; i++)
                {
                    ((ComboBoxItem) cboWMBitrate.Items[index[i]]).ForeColor = Color.Brown;
                }
            }
        }

        private void FormatWMFrequencyCombo(bool reset, params int[] index)
        {
            foreach (ComboBoxItem item in cboWMFrequency.Items)
            {
                item.ForeColor = Color.Black;
            }

            if (!reset)
            {
                for (int i = 0; i < index.Length; i++)
                {
                    ((ComboBoxItem)cboWMFrequency.Items[index[i]]).ForeColor = Color.Brown;
                }
            }
        }

        private void FormatWMChannelsCombo(bool reset, params int[] index)
        {
            foreach (ComboBoxItem item in cboWMChannels.Items)
            {
                item.ForeColor = Color.Black;
            }

            if (!reset)
            {
                for (int i = 0; i < index.Length; i++)
                {
                    ((ComboBoxItem)cboWMChannels.Items[index[i]]).ForeColor = Color.Brown;
                }
            }
        }

        private void DisableAllControls(Control.ControlCollection coll)
        {
            foreach (Control ctrl in coll)
            {
                if (ctrl.GetType() == typeof(Panel))
                {
                    DisableAllControls((ctrl).Controls);
                }

                ctrl.Enabled = false;
            }
        }

        #endregion

        #region Nero AAC Settings.

        private void ConvertNeroAacVbrValues()
        {
            switch (trbNeroAacBitRate.Value)
            {
                case 0:
                    _neroAacBitRate = string.Format(".05");
                    _neroAacBitRateDescription = string.Format("Quality {0} (estimated bit rate: 16 Kbps)", _neroAacBitRate);
                    break;
                case 1:
                     _neroAacBitRate = string.Format(".15");
                    _neroAacBitRateDescription = string.Format("Quality {0} (estimated bit rate: 32 Kbps)", _neroAacBitRate);
                    break;
                case 2:
                    _neroAacBitRate = string.Format(".25");
                    _neroAacBitRateDescription = string.Format("Quality {0} (estimated bit rate: 64 Kbps)", _neroAacBitRate);
                    break;
                case 3:
                    _neroAacBitRate = string.Format(".35");
                    _neroAacBitRateDescription = string.Format("Quality {0} (estimated bit rate: 100 Kbps)", _neroAacBitRate);
                    break;
                case 4:
                    _neroAacBitRate = string.Format(".45");
                    _neroAacBitRateDescription = string.Format("Quality {0} (estimated bit rate: 150 Kbps)", _neroAacBitRate);
                    break;
                case 5:
                    _neroAacBitRate = string.Format(".5");
                    _neroAacBitRateDescription = string.Format("Quality {0} (estimated bit rate: 170 Kbps)", _neroAacBitRate);
                    break;
                case 6:
                    _neroAacBitRate = string.Format(".55");
                    _neroAacBitRateDescription = string.Format("Quality {0} (estimated bit rate: 200 Kbps)", _neroAacBitRate);
                    break;
                case 7:
                    _neroAacBitRate = string.Format(".65");
                    _neroAacBitRateDescription = string.Format("Quality {0} (estimated bit rate: 250 Kbps)", _neroAacBitRate);
                    break;
                case 8:
                    _neroAacBitRate = string.Format(".75");
                    _neroAacBitRateDescription = string.Format("Quality {0} (estimated bit rate: 300 Kbps)", _neroAacBitRate);
                    break;
                case 9:
                    _neroAacBitRate = string.Format(".85");
                    _neroAacBitRateDescription = string.Format("Quality {0} (estimated bit rate: 350 Kbps)", _neroAacBitRate);
                    break;
                case 10:
                    _neroAacBitRate = string.Format(".95");
                    _neroAacBitRateDescription = string.Format("Quality {0} (estimated bit rate: 400 Kbps)", _neroAacBitRate);
                    break;
                case 11:
                    _neroAacBitRate = string.Format("1.00");
                    _neroAacBitRateDescription = string.Format("Quality {0} (estimated bit rate: 425 Kbps)", _neroAacBitRate);
                    break;
            }

            _neroAacBitRateValue = _neroAacBitRate;

            lblNeroAacQualityInfo.Text = _neroAacBitRateDescription;
        }

        private void ConvertNeroAacAbrCbrValues()
        {
            switch (trbNeroAacBitRate.Value)
            {
                case 0:
                    _neroAacBitRate = string.Format("16");
                    break;
                case 1:
                    _neroAacBitRate = string.Format("24");
                    break;
                case 2:
                    _neroAacBitRate = string.Format("32");
                    break;
                case 3:
                    _neroAacBitRate = string.Format("48");
                    break;
                case 4:
                    _neroAacBitRate = string.Format("56");
                    break;
                case 5:
                    _neroAacBitRate = string.Format("64");
                    break;
                case 6:
                    _neroAacBitRate = string.Format("80");
                    break;
                case 7:
                    _neroAacBitRate = string.Format("96");
                    break;
                case 8:
                    _neroAacBitRate = string.Format("128");
                    break;
                case 9:
                    _neroAacBitRate = string.Format("144");
                    break;
                case 10:
                    _neroAacBitRate = string.Format("160");
                    break;
                case 11:
                    _neroAacBitRate = string.Format("192");
                    break;
                case 12:
                    _neroAacBitRate = string.Format("224");
                    break;
                case 13:
                    _neroAacBitRate = string.Format("256");
                    break;
                case 14:
                    _neroAacBitRate = string.Format("320");
                    break;
                case 15:
                    _neroAacBitRate = string.Format("400");
                    break;
            }

            _neroAacBitRateValue = string.Format("{0}000", _neroAacBitRate);

            if (rbtNeroAacBitrateAbr.Checked)
            {
                lblNeroAacQualityInfo.Text = string.Format("Average Bit Rate: {0} Kbps", _neroAacBitRate);
            }
            else
            {
                lblNeroAacQualityInfo.Text = string.Format("Constant Bit Rate: {0} Kbps", _neroAacBitRate);
            }
        }

        #endregion

        #region Commented Code.

        //private void Convert()
        //{
        //    //var frm = new FormProgressWorker();
        //    //frm.Text = @"Convert Audio Files";

        //    var args = new DoWorkConvertAudioEventArgs(null);
        //    args.Files = _convertFiles;

        //    if (rbtFolder.Checked && Directory.Exists(txtFolder.Text))
        //    {
        //        args.ToPath = txtFolder.Text;
        //    }
        //    else
        //    {
        //        args.ToPath = Path.GetDirectoryName(args.Files[0]);
        //    }

        //    switch (cboCompressionCodecs.Text)
        //    {
        //        case "mp3 (Lame)":
        //            args.Setting = GetMp3ConversionSetting();
        //            args.Extension = "mp3";
        //            args.Codec = "mp3 (Lame)";
        //            break;
        //        case "FLAC":
        //            args.Setting = GetFlacConversionSetting();
        //            args.Extension = "flac";
        //            args.Codec = "FLAC";
        //            break;
        //        case "Shorten":
        //            args.Setting = GetShortenConversionSettings();
        //            args.Extension = "shn";
        //            args.Codec = "Shorten";
        //            break;
        //        case "Apple Lossless":
        //            args.Setting = GetAppleLosslessConversionSettings();
        //            args.Extension = "m4a";
        //            args.Codec = "Apple Lossless";
        //            break;
        //        case "Aiff":
        //            args.Setting = GetAiffConversionSettings();
        //            args.Extension = "aif";
        //            args.Codec = "Aiff";
        //            break;
        //    }

        //    _coreCounter = GetNumberOfCores();

        //    int currentCore = 0;

        //    for (int index = 0; index < args.Files.Count; index++)
        //    {

        //        string fromFile = args.Files[index];

        //        if (currentCore > _coreCounter)
        //        {
        //            currentCore = 1;
        //        }
        //        else
        //        {
        //            currentCore++;
        //        }

        //        string filename = string.Format("{0}.{1}", Path.GetFileNameWithoutExtension(fromFile), args.Extension);
        //        string path = Path.GetDirectoryName(fromFile);
        //        if (path != null)
        //        {
        //            string toFile = Path.Combine(path, filename);
        //            Convert(fromFile, toFile, args.Codec, args.Setting, currentCore.ToString());
        //        }

        //    }

        //    btnConvert.Text = @"Finished";
        //    btnConvert.DialogResult = DialogResult.OK;
        //    //frm.DoWorkConvertAudio += (sender, e) => ProgressForm_DoWorkConvertAudio(frm, args);
        //    //StartForm(frm);
        //}

        //public void Convert(string infile, string outfile, string convertTo, string cliSettings, string processor)
        //{
        //    string ifile = string.Format("-infile=\"{0}\"", infile);
        //    string ofile = string.Format("-outfile=\"{0}\"", outfile);
        //    string dspef = AddDspEffects();
        //    string cnvto = string.Format("-convert_to=\"{0}\"", convertTo);
        //    string procs = string.Format("-processor=\"{0}\"", processor);
        //    string clist = string.Format("{0}", cliSettings);

        //    //cnvto = string.Format("{0} {1}", cnvto, procs);

        //    if (!chkPreserveTags.Checked)
        //    {
        //        cnvto = string.Format("{0} -noidtag", cnvto);
        //    }

        //    string args = string.Format(" {0} {1} {2} {3} {4}", ifile, ofile, dspef, cnvto, clist);

        //    _startInfo.Arguments = args;

        //    process.StartInfo = _startInfo;

        //    process.Start();

        //    process.WaitForExit(5000);
        //}

        //private void GetAllCompressionCodecs()
        //{
        //    for (int i = 0; ; i++)
        //    {
        //        string codec = _dmc.GetCompressions[i];

        //        if (codec.Length > 0)
        //        {
        //            switch (codec)
        //            {
        //                case "[Multi Encoder]":
        //                    //cboCompressionCodecs.Add(AudioConverterResource._Multi_Encoder_, codec);
        //                    break;
        //                case "Aiff":
        //                    //cboCompressionCodecs.Add(imlCodecs.Images["Aiff"], codec);
        //                    break;
        //                case "Apple Lossless":
        //                    //cboCompressionCodecs.Add(imlCodecs.Images["Apple Lossless"], codec);
        //                    break;
        //                case "FLAC":
        //                    //cboCompressionCodecs.Add(imlCodecs.Images["FLAC"], codec);
        //                    break;
        //                case "mp3 (Lame)":
        //                    //cboCompressionCodecs.Add(imlCodecs.Images["mp3 (Lame)"], codec);
        //                    break;
        //                case "ogg vorbis":
        //                    //cboCompressionCodecs.Add(AudioConverterResource.ogg_vorbis, codec);
        //                    break;
        //                case "Shorten":
        //                    cboCompressionCodecs.Add(imlCodecs.Images["Shorten"], codec);
        //                    break;
        //                case "Test Conversion":
        //                    //cboCompressionCodecs.Add(AudioConverterResource.Test_Conversion, codec);
        //                    break;
        //                case "Wave":
        //                    //cboCompressionCodecs.Add(AudioConverterResource.Wave, codec);
        //                    break;
        //                case "Windows Media Audio 10":
        //                    //cboCompressionCodecs.Add(AudioConverterResource.Windows_Media_Audio_10, codec);
        //                    break;
        //                case "m4a Nero (AAC)":
        //                    //cboCompressionCodecs.Add(AudioConverterResource.m4a_Nero__AAC_, codec);
        //                    break;
        //            }
        //        }
        //        else
        //        {
        //            break;
        //        }
        //    }
        //}

        //private string GetInstalledShortenDecoder()
        //{
        //    var splitter = new []{" -"};
        //    string value;
        //    string path = string.Empty;

        //    try
        //    {
        //        _modifyRegistry.BaseRegistryKey = Registry.CurrentUser;
        //        _modifyRegistry.SubKey = "Software\\Illustrate\\dBpoweramp\\dMCCodec\\Shorten";

        //        value = _modifyRegistry.Read("dMC_CLI");
        //    }
        //    catch (Exception)
        //    {
        //        return string.Empty;
        //    }


        //    if (string.IsNullOrEmpty(value))
        //    {
        //        return string.Empty;
        //    }

        //    string[] split = value.Split(splitter, StringSplitOptions.RemoveEmptyEntries);

        //    if (split.Length > 0)
        //    {
        //        _cliEncoder = split[0];
        //        path = _cliEncoder.Replace("cli_encoder=", "");
        //        path = path.Replace("\"", "");
        //    }
        //    else
        //    {
        //        _cliEncoder = string.Empty;
        //    }

        //    if (split.Length > 1)
        //    {
        //        _cliCmd = split[1];
        //    }
        //    else
        //    {
        //        _cliCmd = string.Empty;
        //    }

        //    if (split.Length > 2)
        //    {
        //        _selection = split[2];
        //    }
        //    else
        //    {
        //        _selection = string.Empty;
        //    }

        //    return path;    
        //}

        //private bool SetInstalledShortenDecoder()
        //{
        //    try
        //    {
        //        _modifyRegistry.BaseRegistryKey = Registry.CurrentUser;
        //        _modifyRegistry.SubKey = "Software\\Illustrate\\dBpoweramp\\dMCCodec\\Shorten";
        //        _modifyRegistry.Write("dMC_CLI", GetShortenConversionSettings());

        //        return true;
        //    }
        //    catch (Exception)
        //    {
        //        return false;
        //    }
        //}

        //private void ProcessOutputHandler(object sender, ProcessOutputEventArgs args)
        //{
        //    // Collect the sort command output. 
        //    if (!String.IsNullOrEmpty(args.Data))
        //    {
        //        if (richTextBoxConsole.InvokeRequired)
        //        {
        //            richTextBoxConsole.Invoke((MethodInvoker)(() => richTextBoxConsole.AppendText(args.Data)));
        //        }
        //        else
        //        {
        //            richTextBoxConsole.AppendText(args.Data);
        //        }

        //        //numOutputLines++;

        //        // Add the text to the collected output.
        //        //_output.Append(Environment.NewLine +
        //        //    "[" + numOutputLines.ToString() + "] - " + outLine.Data);
        //    }
        //}

        //private void ProgressForm_DoWorkConvertAudio(FormProgressWorker sender, DoWorkEventArgs e)
        //{
        //    var args = (DoWorkConvertAudioEventArgs)e;
        //    int progress = 1;
        //    int total = args.Files.Count;

        //    foreach (string fromFile in args.Files)
        //    {
        //        string filename = string.Format("{0}.{1}", Path.GetFileNameWithoutExtension(fromFile), args.Extension);
        //        string toFile = Path.Combine(args.ToPath, filename);

        //        int percent = progress * 100 / total;
        //        string status = string.Format("Convert audio to {0}, file {1} of {2}", args.Codec, progress, total);

        //        _dmc.Convert(fromFile, toFile, args.Codec, args.Setting, "");
        //        //cc.Convert(fromFile, toFile, args.Codec, args.Setting);

        //        if (args.Codec == "Shorten")
        //        {
        //            sender.SetProgress(percent, status, 300);
        //        }
        //        else if (args.Codec == "Aiff")
        //        {
        //            sender.SetProgress(percent, status, 60);
        //        }
        //        else
        //        {
        //            sender.SetProgress(percent, status, 30);
        //        }

        //        progress++;

        //        //while (sender.IsBusy)

        //        if (sender.CancellationPending)
        //        {
        //            e.Cancel = true;
        //            return;
        //        }
        //    }

        //    if (btnConvert.InvokeRequired)
        //    {
        //        btnConvert.BeginInvoke(new Action(() => { btnConvert.Text = @"Finished"; btnConvert.DialogResult = DialogResult.OK;}));
        //    }
        //    else
        //    {
        //        btnConvert.Text = "Finished";
        //        btnConvert.DialogResult = DialogResult.OK;
        //    }
        //}

        //private void StartForm(FormProgressWorker form)
        //{
        //    DialogResult result = form.ShowDialog();

        //    if (result == DialogResult.Cancel)
        //    {
        //        MessageBox.Show(this, @"The operation has been cancelled by the user!", @"Information",
        //                        MessageBoxButtons.OK, MessageBoxIcon.Information);
        //    }

        //    if (result == DialogResult.Abort)
        //    {
        //        string message = string.Format("There was a problem with the current operation\n\n{0}",
        //                                       form.Result.Error.Message);

        //        MessageBox.Show(this, message, @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //    }
        //}

        #endregion
    }
}
